-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.最长公共前缀.cpp
50 lines (43 loc) · 1.03 KB
/
14.最长公共前缀.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode.cn id=14 lang=cpp
*
* [14] 最长公共前缀
*/
// @lc code=start
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result;
if(strs.size() == 0)
{
return result;
}
else
{
result = strs.at(0);
}
for (int i = 1; i < strs.size(); i++)
{
string middleResult;
string otherString = strs.at(i);
for (int iter = 0; iter < otherString.size() && iter < result.size(); iter++)
{
if (otherString.at(iter) != result.at(iter))
{
break;
}
else
{
middleResult += result.at(iter);
}
}
result = middleResult;
if(result.size() == 0)
{
break;
}
}
return result;
}
};
// @lc code=end