-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-LongestCommonPrefix.cpp
50 lines (45 loc) · 1.25 KB
/
14-LongestCommonPrefix.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
/*=============================================================================
# FileName: 14-LongestCommonPrefix.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-04-05 05:06:51
# Version: 0.0.1
# LastChange: 2015-04-05 05:06:51
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
class Solution {
public:
//7ms
string longestCommonPrefix(vector<string> &strs) {
string result = "";
int minLength = INT_MAX;
if (strs.size() == 0) {
return result;
}
for (int i = 0; i < strs.size(); i ++) {
if (strs[i] == "") {
return result;
}
minLength = min(minLength, (int)(strs[i].length()));
}
for (int i = 0; i < minLength; i ++) {
for (int j = 1; j < strs.size(); j ++) {
if (strs[j][i] != strs[j - 1][i]) {
return result;
}
}
result += strs[0][i];
}
return result;
}
};
int main() {
return 0;
}