leetcode6: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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string s;
if( strs.size()<1) return s;
if( strs.size()==1) {s=strs[0]; return s;}
s = "";
for(int i=0; i!= strs[0].size(); ++i){
const char c = strs[0][i];
for(int j=0; j!= strs.size(); ++j) {
if( i >= strs[j].size() || strs[j][i] != c) return s;
}
s.push_back( c );
}
return s;
}
};
浙公网安备 33010602011771号