Created
February 28, 2019 01:44
-
-
Save qiaoxu123/7aec12a37190b5d7b07f0516f52ab1fd to your computer and use it in GitHub Desktop.
> 一个简答的字符串遍历和统计题目 题目本身不难,但是之前一直没有自己认真写过,所以花了好多时间。 大体思路是,统计字符串数组中每一个前缀字母的个数,使用遍历的方法。
This file contains hidden or 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
//Runtime: 8 ms, faster than 99.70% | |
//Memory Usage: 9.5 MB, less than 74.22% | |
class Solution { | |
public: | |
string longestCommonPrefix(vector<string>& strs) { | |
if(strs.size() == 0) return ""; | |
string str; | |
int count = 0; | |
int j = 0; | |
str = ""; | |
while(1){ | |
for(int i = 0;i < strs.size();++i){ | |
if(strs[i] == "") return ""; | |
if(strs[0][j] == strs[i][j]) | |
count++; | |
if(count == strs.size()) | |
str += strs[i][j]; | |
if(j == strs[i].length()) | |
return str; | |
} | |
if(count < strs.size()) | |
return str; | |
j++; | |
count = 0; | |
} | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment