Leetcode Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.empty()) return "";
int minLength = strs[].length();
for(int i = ; i < strs.size(); ++ i) minLength = min(minLength,(int)strs[i].length());
bool flag = false;
int i = ;
for(i = ; i < minLength; ++ i){
char ch = strs[][i];
for(int j = ; j < strs.size(); ++ j){
if(strs[j][i]!=ch){flag=true;break;}
}
if(flag) break;
}
return strs[].substr(,i);
}
};
上一篇:[LintCode] Longest Common Prefix 最长共同前缀


下一篇:14. Longest Common Prefix