码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 之Longest Common Prefix(33)

时间:2016-05-25 18:39:27      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

在一组字符串中找到最长的子串。采用纵向匹配,遇到第一个不匹配的停止。

技术分享
string longestComPrefix(vector<string> &strs)
      {
          if (strs.empty())return " ";

          //纵向比较
          for (int idx = 0; idx < strs[0].size(); idx++)
          {
              for (int i = 0; i < strs.size(); i++)
              {
                  if (strs[0][idx] != strs[i][idx])return strs[0].substr(0, idx);
              }
          }
          return strs[0];
      }
View Code

 

Leetcode 之Longest Common Prefix(33)

标签:

原文地址:http://www.cnblogs.com/573177885qq/p/5527704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!