# leetcode 014 —— 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) {
int n = strs.size();
if (n == 0)
return "";
if (n == 1)
return strs[0];
int min = strs[0].size();
for (int i = 1; i<strs.size(); i++)   //找到最短字符串
min = strs[i].size()<min ? strs[i].size() : min;

int i, j = 0;
string s = "";
for (i = 0; i < min; i++){  //string的元素坐标

for (int j = 0; j < n - 1; j++){  //string的坐标,判断是否有相同的
if (strs[j][i] != strs[j + 1][i])
{
return s;

}
}
s = strs[0].substr(0, i + 1);
}
return s;
}
};

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

2015-10-16 22:05:46

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

2016-09-04 18:33:06

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

2016-06-15 13:12:37

#### [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] 014. Longest Common Prefix (Easy) (C++/Java/Python)

2015-03-03 15:34:34

#### LeetCode 14 — Longest Common Prefix（C++ Java Python）

2014-04-03 18:42:35

#### LeetCode_Easy心得：14. Longest Common Prefix（C语言）

2017-07-22 12:05:38

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

2017-03-27 10:49:05