-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLongest-Common-Prefix.cpp
53 lines (49 loc) · 1.42 KB
/
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
/// Source : https://leetcode-cn.com/problems/longest-common-prefix/
/// Author : bryce
/// Time : 2019-11-05
//C++ Solution 1:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int n = strs.size();
int i = 0,b = 0;
if (n == 0) return "";
string res = "";
int min_length = strs[0].size();
for (i = 0; i < n; i++)
{
int m = strs[i].size();
min_length = min(min_length,m);
if (min_length >= m) b = i;
}
if (min_length==0) return "";
res = strs[b];
for (int l = 0; l < n; l++)
{
for(int j = 0; j < min_length;j++)
{
if (res[j] != strs[l][j])
res = res.substr(0,j);
}
}
return res;
}
};
// C++ Solution 2:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0)
return "";
int min_length = strs[0].length();
for (int i = 1; i < strs.size(); i++)
min_length = min(min_length, int(strs[i].length()));
for (int s = 1; s <= min_length; s++) {
char c = strs[0][s - 1];
for (int i = 1; i < strs.size(); i++)
if (strs[i][s - 1] != c)
return strs[0].substr(0, s - 1);
}
return strs[0].substr(0, min_length);
}
};