1 class Solution {
2 public:
3 string longestCommonPrefix(vector<
string> &
strs) {
4 // Start typing your C/C++ solution below
5 // DO NOT write int main() function
6 string result;
7 if (strs.size() ==
0)
return result;
8 if (strs.size() ==
1){
9
10 if (strs[
0].size() ==
0)
return result;
11 result.push_back(strs[
0][
0]);
12 return result;
13 }
14
15 int minLength =
INT_MAX;
16 for (
int i=
0; i<strs.size(); i++
){
17
18 if (strs[i].size() ==
0)
return result;
19 else if (strs[i].size() < minLength) minLength =
strs[i].size();
20 }
21
22 for (
int j =
0; j<minLength; j++
)
23 for (
int i =
0; i<strs.size()-
1; i++
){
24
25 if (strs[i][j] == strs[i+
1][j]){
26
27 if (i == strs.size()-
2) result.push_back(strs[i][j]);
28 }
29
30 else return result;
31 }
32 }
33 };
转载于:https://www.cnblogs.com/tanghulu321/archive/2013/05/13/3074990.html
相关资源:数据结构—成绩单生成器