-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.cpp
39 lines (36 loc) · 931 Bytes
/
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
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) {
return "";
}
string prefix = "";
int p = 0;
int size = strs.size();
bool loop = true;
while (loop) {
char c = '\0';
for (int i = 0; i < size; ++i) {
if (p >= strs[i].size()) {
loop = false;
break;
}
if (c == '\0') {
c = strs[i][p];
}
else if (c == strs[i][p]) {
continue;
}
else {
loop = false;
break;
}
}
if (loop) {
prefix.push_back(c);
++p;
}
}
return prefix;
}
};