Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created October 14, 2018 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/968c08e25edce4016e1bd0d4d587f4a7 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/968c08e25edce4016e1bd0d4d587f4a7 to your computer and use it in GitHub Desktop.
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int len_s = strs.size();
if(len_s == 0) return "";
string prefix = strs[0];
for(int i = 1; i < len_s; i++){
int minlen = min(prefix.size(), strs[i].size());
int j = 0;
for(; j < minlen; j++){
if(prefix[j] != strs[i][j])
break;
}
prefix = strs[0].substr(0, j);
}
return prefix;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment