Write a function to find the longest common prefix string amongst an array of strings.
Have you met this question in a real interview?
Yes
No
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int i,j;
char c;
string ans="";
if(strs.size()==0) return ans; //注意判断为空的情况
for(i=0; ;i++){
if(i>=strs[0].length()) return ans;
c=strs[0][i];
for(j=1;j<strs.size();j++){
if(i>=strs[j].length()||c!=strs[j][i]){
return ans;
}
}
ans+=c;
}
return ans;
}
};