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

LeetCode – Refresh – Longest Common Prefix

时间:2015-03-20 08:06:00      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

Seach one by one.

 1 class Solution {
 2 public:
 3     string getNext(const string& s1, const string& s2) {
 4         int len = min(s1.size(), s2.size()), i = 0;
 5         for (; i < len; i++) {
 6             if (s1[i] != s2[i]) {
 7                 break;
 8             }
 9         }
10         return s1.substr(0, i);
11     }
12     string longestCommonPrefix(vector<string> &strs) {
13         int len = strs.size();
14         if (len == 0) return "";
15         string result = strs[0];
16         for (int i = 1; i < len; i++) {
17             result = getNext(result, strs[i]);
18         }
19         return result;
20     }
21 };

 

LeetCode – Refresh – Longest Common Prefix

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352678.html

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