Skip to content

Commit f39dd3b

Browse files
committed
.\longestCommonPrefix.cpp
1 parent 152332e commit f39dd3b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

LeetCode/longestCommonPrefix.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
#define REP(i,n) for(int i=0;i<(n);++i)
6+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
7+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
8+
9+
class Solution {
10+
public:
11+
string longestCommonPrefix(vector<string> &strs) {
12+
if (strs.empty()) return "";
13+
REP(i,strs[0].length()) {
14+
FOR(j,1,strs.size()-1) {
15+
if (i >= strs[j].length() || strs[0][i] != strs[j][i]) return strs[0].substr(0, i);
16+
}
17+
}
18+
return strs[0];
19+
}
20+
};
21+
22+
int main() {
23+
vector<string> mm;
24+
mm.push_back("abc");
25+
mm.push_back("abd");
26+
mm.push_back("aaa");
27+
Solution s = Solution();
28+
cout << s.longestCommonPrefix(mm) << endl;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)