Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created October 14, 2018 03:05
Show Gist options
  • Save zhangxiaomu01/51656b0a309ceb734abd0d664093c9d8 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/51656b0a309ceb734abd0d664093c9d8 to your computer and use it in GitHub Desktop.
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int len = strs.size();
string finalStr;
if(len == 0) return "";
sort(strs.begin(), strs.end());
int n = strs[0].size();
for(int i = 0; i<n; i++)
{
if(strs[0][i] == strs[len-1][i]){
finalStr += strs[0][i];
}
else
break;
}
return finalStr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment