Article Directory
THE
problem
solution
Code
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans ="" ;
if(!strs.size()) return ans;
if(strs.size()==1) return strs[0];
int len = strs.size();
for(int i = 0 ; i<strs[0].size();i++){
//cout<<strs[i];
for(int j = 0 ; j<len;j++){
if(strs[0][i]!=strs[j][i])return ans;
}
ans = ans +strs[0][i];
}
return ans;
}
};
Summary and reflection
- Pay attention to consideration of boundary issues. Comparative problems are more basic. There is no combined algorithm.