标签:
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