forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0014.cpp
More file actions
28 lines (23 loc) · 703 Bytes
/
0014.cpp
File metadata and controls
28 lines (23 loc) · 703 Bytes
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
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() == 0) return "";
if (strs.size() == 1) return strs[0];
string ans;
int minLen = strs[0].length();
for (int i = 1; i < strs.size(); i++)
minLen = min(minLen, (int)strs[i].length());
bool isMatch = true;
for (int i = 0; i < minLen; i++) {
char c = strs[0][i];
for (int j = 1; j < strs.size(); j++)
if (c != strs[j][i]) {
isMatch = false;
break;
}
if (!isMatch) break;
ans += c;
}
return ans;
}
};