###### [LeetCode] Longest Common Prefix

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int i = 1;
string com_s;
if(strs.size()==0)  return string("");
if(strs.size()==1)  return strs[0];
com_s = strs[0];
while(i!=strs.size())
com_s = check_two(com_s,strs[i++]);
return com_s;

}
string check_two(string &s1, string &s2){
int i;
for(i = 0; i != min(s1.size(),s2.size()); ++i)
if(s1[i]!=s2[i])
break;
return s1.substr(0,i);
}
};

#define min(x,y) (x)<(y)?(x):(y)
char* check_two(char *s1, char *s2){
int i =0;
char *tmp;
if(strlen(s1)>strlen(s2)){
tmp = s1;
s1 = s2;
s2 = tmp;
}
for(i = 0; i != strlen(s1); ++i)
if(s1[i]!=s2[i])
break;
if(i!=strlen(s1))
s1[i] = '\0';
return s1;

}
char* longestCommonPrefix(char** strs, int strsSize) {
char *com_s = strs[0];
int i =1;
com_s = check_two(com_s,strs[i++]);
return com_s;
}



#### LeetCode 14 Longest Common Prefix (C,C++,Java,Python)

2015-05-08 18:40:35

#### [LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

2016-06-15 13:12:37

#### LeetCode 14 Longest Common Prefix（最长公共前缀）（String）

2015-10-16 22:05:46

#### [leetcode:python]14.Longest Common Prefix

2017-05-10 19:07:20

#### [LeetCode]Longest Common Prefix

2013-11-22 12:37:41

#### [LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)

2015-03-03 15:34:34

#### 关于LeetCode中Longest Common Prefix一题的理解

2016-09-04 18:33:06

#### [leetcode]14. Longest Common Prefix -- JavaScript 代码

2016-08-30 11:18:31

#### 【LeetCode 14】Longest Common Prefix(Python)

2017-11-07 00:26:06

#### LeetCode 14 Longest Common Prefix C语言实现

2017-03-27 10:49:05

## 不良信息举报

[LeetCode] Longest Common Prefix