-
Notifications
You must be signed in to change notification settings - Fork 0
/
191009-1.cpp
33 lines (31 loc) · 919 Bytes
/
191009-1.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
// https://leetcode-cn.com/problems/longest-common-prefix/
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
const int n = strs.size();
if (n == 0) return "";
const char* s = strs[0].c_str();
const char* p = s;
for (int len = 0; *p; ++len, ++p) {
for (int i = 1; i < n; ++i) {
const char* q = strs[i].c_str() + len;
if (*q != *p) {
return string(s, p);
}
}
}
return strs[0];
}
};
int main() {
Solution s;
vector<string> a1{"flower","flow","flight"};
printf("%s\n", s.longestCommonPrefix(a1).c_str()); // answer: "fl"
vector<string> a2{"dog","racecar","car"};
printf("%s\n", s.longestCommonPrefix(a2).c_str()); // answer: ""
return 0;
}