//需要注意细节
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//sort(strs.begin(),strs.end());
string result("");
if (strs.size()==0) {
return result;
}
int idx = 0;
while(1) {
for(int i=0;i<strs.size();i++) {
//特殊情况的考虑
if(strs[i].length()==0) {
return result;
}
//string.lengt() 返回值类型为size_t
//strs[i].length()-1,当length==0时会出问题
if(idx>strs[i].length()-1) {
return result;
}
}
char ch = strs[0][idx];
for(int i=1;i<strs.size();i++) {
if (ch != strs[i][idx]) {
return result;
}
}
result+=ch;
idx++;
}
return result;
}
};