[leetcode] Longest Common Prefix @ Python
2014-10-13 22:33
316 查看
Source: https://oj.leetcode.com/problems/longest-common-prefix/
Write a function to find the longest common prefix string amongst an array of strings.
Hint: strs=['abc','ab','a']
strs=[
'abc',
'ab',
'a',
]
We can think of strs as a column length varied matrix
Write a function to find the longest common prefix string amongst an array of strings.
Hint: strs=['abc','ab','a']
strs=[
'abc',
'ab',
'a',
]
We can think of strs as a column length varied matrix
class Solution: # @return a string def longestCommonPrefix(self, strs): #横向扫描,每个字符串与第0 个字符串,从左到右比较,直到遇到一个不匹配, #然后继续下一个字符串 #时间复杂度O(n1+n2+...) if len(strs) == 0: return "" minL = min([len(word) for word in strs]) for j in range(minL): for i in range(1, len(strs)): if strs[i][j] != strs[0][j]: return strs[0][:j] return strs[0][:minL]
相关文章推荐
- leetcode:Longest Common Prefix【Python版】
- Longest Common Prefix Leetcode Python Java
- [LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)
- LeetCode 14 — Longest Common Prefix(C++ Java Python)
- [LeetCode]题解(python):014-Longest Common Prefix
- [leetcode]Longest Common Prefix @ Python
- 【Leetcode】【python】Longest Common Prefix
- python写算法题:leetcode: 14. Longest Common Prefix
- leetcode Longest Common Prefix python
- [leetcode:python]14.Longest Common Prefix
- [leetCode By Python] 14. Longest Common Prefix
- 【LeetCode 14】Longest Common Prefix(Python)
- LeetCode 14 Longest Common Prefix (C,C++,Java,Python)
- 【LeetCode with Python】 Longest Common Prefix
- Leetcode-Longest Common Prefix-Python
- LeetCode: Longest Common Prefix
- Longest Common Prefix(寻找最长前缀) 【leetcode】
- Leetcode: Longest Common Prefix
- leetcode -- Longest Common Prefix
- [LeetCode]Longest Common Prefix