Created
October 14, 2018 01:55
-
-
Save zhangxiaomu01/991f56aa6f916d21b84c103f60fae535 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 ""; | |
for(int j = 0; j < strs[0].size(); j++){ | |
char c = strs[0][j]; | |
for(int i = 0; i < len_s; i++){ | |
//Basically we check two cases: | |
//1 The sub string i reaches its end, which means this should be the common prefix | |
//2 The current element is not equal to the common element any more | |
if(j == strs[i].size() || strs[i][j] != c) | |
return strs[0].substr(0, j); | |
} | |
} | |
return strs[0]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment