【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

Solution:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) { //runtime:4ms
string str="";
if(strs.empty())return str;
int index=; while(true){
if(index>=strs[].size())return str;
char c=strs[][index];
for(int i=;i<strs.size();i++){
if(index>=strs[i].size()||strs[i][index]!=c)return str;
}
str+=c;
index++;
} }
};
上一篇:【LeetCode】14. Longest Common Prefix 最长前缀子串


下一篇:hdu_4497GCD and LCM(合数分解)