-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path014.Longest Common Prefix.cpp
54 lines (50 loc) · 1.47 KB
/
014.Longest Common Prefix.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
50
51
52
53
54
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string prefix = "";
if(strs.size() == 0)
return prefix;
if(strs.size() == 1)
return strs[0];
int pos = 0;
if(pos >= strs[0].size())
return prefix;
while(1){
for(int i = 1; i < strs.size(); ++i){
if(pos < strs[i].size()){
if(strs[i][pos] != strs[i-1][pos])
return prefix;
}else {
return prefix;
}
}
prefix += strs[0][pos++];
}
}
};
// 分治法
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0) return "";
return longestCommonPrefix(strs, 0, strs.size()-1);
}
string longestCommonPrefix(vector<string>& strs, int l, int r){
if(l == r)
return strs[l];
else{
int m = (l + r) / 2;
string lcpLeft = longestCommonPrefix(strs, l, m);
string lcpRight = longestCommonPrefix(strs, m+1, r);
return commonPrefix(lcpLeft, lcpRight);
}
}
string commonPrefix(string left, string right){
int length = min(left.size(), right.size());
for(int i = 0; i < length; ++i){
if(left[i] != right[i])
return left.substr(0, i);
}
return left.substr(0, length);
}
};