https://leetcode.com/problems/longest-common-prefix/description/
string longestCommonPrefix(vector<string>& strs) {
if(strs.size()==0 || strs[0].size() == 0) return "";
for(int j=0; j<strs[0].size(); j++) {
for(int i=1; i<strs.size(); i++) {
if(j>=strs[i].size() || strs[0][j] != strs[i][j]) {
return strs[0].substr(0,j);
}
}
}
return strs[0];
}
No comments:
Post a Comment