Maple's Blog.

Leetcode-14

字数统计: 80阅读时长: 1 min
2019/04/09 Share

longestCommonPrefix-链接

实现的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {

string ans="";
if(strs.size()==0)
return ans;
char m;
for(int i=0;i<strs[0].size();i++)
{
m=strs[0][i];
for(int j=1;j<strs.size();j++)
{
if(i>=strs[j].size()||m!=strs[j][i])
return ans;
}
ans+=m;
}
return ans;
}
};
CATALOG