title | toc | date | tags | top | ||
---|---|---|---|---|---|---|
14. Longest Common Prefix |
false |
2017-10-30 |
|
14 |
Write a function to find the longest common prefix string amongst an array of strings.
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
# 空的列表
if strs == []:
return ""
lcp = ""
small_length = len(strs[0])
for str in strs:
if len(str) < small_length:
small_length = len(str)
# 全部都是空字符串
if small_length == 0:
return ""
print(small_length)
for i in range(small_length):
lcp += strs[0][i]
for str in strs:
# 该字符不符合最大子字符串
if str[i] != lcp[i]:
return lcp[0:i]
#全部符合
return lcp
```