LeetCode – Refresh – Longest Common Prefix

时间:2015-03-20 08:04:03   收藏:0   阅读:235

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 };

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!