Created
October 14, 2018 02:14
-
-
Save zhangxiaomu01/968c08e25edce4016e1bd0d4d587f4a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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