-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-LongestCommonPrefix.cpp
35 lines (31 loc) · 1.04 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
/*=============================================================================
# FileName: 14-LongestCommonPrefix.cpp
# Desc: AC, 4ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-02 15:56:10
# History:
=============================================================================*/
#include <leetcode.h>
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int len = strs.size(), index = 0;
string result = "";
if (len == 0) {
return result;
}
while (true) {
char c = strs[0][index];
for (int i = 0; i < len; i ++) {
if ((int)strs[i].length() == index || strs[i][index] != c) {
return result;
}
}
result += c;
index ++;
}
}
};