###### LeetCode Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result = "";
if(strs.size() == 1)
return strs[0];
for(int ind =0; strs.size() > 0;result+=strs[0][ind],ind++)
for(int i=1;i<strs.size();i++)
if(ind >= strs[i].size() || strs[0][ind] != strs[i][ind])
return result;
return result;
}
};

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result = "";

if (strs.size() > 0){

sort(strs.begin(),strs.end());

string a = strs[0];
string b = strs[strs.size()-1];

for (int i = 0; i < a.length(); i ++){
if (b.length() > i && b[i] == a[i]){
result += b[i];
}
else {
return result;
}
}
}
return result;
}
};

#### [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 14 — Longest Common Prefix（C++ Java Python）

2014-04-03 18:42:35

#### [LeetCode]Longest Common Prefix

2013-11-22 12:37:41

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

2015-05-08 18:40:35

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

2017-05-10 19:07:20

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

2016-09-04 18:33:06

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

2015-03-03 15:34:34

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

2017-03-27 10:49:05

#### [C++]LeetCode 14: Longest Common Prefix（最长公共前缀）

2015-05-02 19:19:45

## 不良信息举报

LeetCode Longest Common Prefix