LeetCode Everyday:堅持價值投資,做時間的朋友!!!
題目:
編寫一個函數來查找字符串數組中的最長公共前綴。
如果不存在公共前綴,返回空字符串""
。
示例:
- 示例 1:
輸入: ["flower","flow","flight"] 輸出: "fl"
- 示例 2:
輸入: ["dog","racecar","car"] 輸出: "" 解釋: 輸入不存在公共前綴。
代碼
方法一: 橫向掃描
- 時間複雜度:O(mn)
- 空間複雜度:O(1)
執行用時 :44 ms, 在所有 Python3 提交中擊敗了59.87%的用戶
內存消耗 :13.7 MB, 在所有 Python3 提交中擊敗了6.15%的用戶
class Solution:
def lcp(self, str1, str2):
length, index = min(len(str1), len(str2)), 0
while index < length and str1[index] == str2[index]:
index += 1
return str1[:index]
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
prefix, count = strs[0], len(strs)
for i in range(1, count):
prefix = self.lcp(prefix, strs[i])
if not prefix:
break
return prefix
"""
For Example: input: strs = ["flower","flow","flight"]
output: "fl"
"""
strs = ["flower","flow","flight"]
solution = Solution()
result = solution.longestCommonPrefix(strs)
print('輸出爲:', result) # f1
方法二: 縱向掃描
- 時間複雜度:O(mn)
- 空間複雜度:O(1)
執行用時 :44 ms, 在所有 Python3 提交中擊敗了59.87%的用戶
內存消耗 :13.7 MB, 在所有 Python3 提交中擊敗了6.15%的用戶
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
length, count = len(strs[0]), len(strs)
for i in range(length):
c = strs[0][i]
if any(i == len(strs[j]) or strs[j][i] != c for j in range(1, count)):
return strs[0][:i]
return strs[0]
"""
For Example: input: strs = ["flower","flow","flight"]
output: "fl"
"""
strs = ["flower","flow","flight"]
solution = Solution()
result = solution.longestCommonPrefix(strs)
print('輸出爲:', result) # f1
方法三: 分治
- 時間複雜度:O(mn)
- 空間複雜度:O(mlogn)
執行用時 :40 ms, 在所有 Python3 提交中擊敗了79.86%的用戶
內存消耗 :13.9 MB, 在所有 Python3 提交中擊敗了6.15%的用戶
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
def lcp(start, end):
if start == end:
return strs[start]
mid = (start + end) // 2
lcpLeft, lcpRight = lcp(start, mid), lcp(mid + 1, end)
minLength = min(len(lcpLeft), len(lcpRight))
for i in range(minLength):
if lcpLeft[i] != lcpRight[i]:
return lcpLeft[:i]
return lcpLeft[:minLength]
return "" if not strs else lcp(0, len(strs) - 1)
"""
For Example: input: strs = ["flower","flow","flight"]
output: "fl"
"""
strs = ["flower","flow","flight"]
solution = Solution()
result = solution.longestCommonPrefix(strs)
print('輸出爲:', result) # f1
方法四: 二分查找
- 時間複雜度:O(mnlogm)
- 空間複雜度:O(1)
執行用時 :44 ms, 在所有 Python3 提交中擊敗了59.87%的用戶
內存消耗 :13.8 MB, 在所有 Python3 提交中擊敗了6.15%的用戶
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
def isCommonPrefix(length):
str0, count = strs[0][:length], len(strs)
return all(strs[i][:length] == str0 for i in range(1, count))
if not strs:
return ""
minLength = min(len(s) for s in strs)
low, high = 0, minLength
while low < high:
mid = (high - low + 1) // 2 + low
if isCommonPrefix(mid):
low = mid
else:
high = mid - 1
return strs[0][:low]
"""
For Example: input: strs = ["flower","flow","flight"]
output: "fl"
"""
strs = ["flower","flow","flight"]
solution = Solution()
result = solution.longestCommonPrefix(strs)
print('輸出爲:', result) # f1
方法五: 最小最大字符串
先找出數組中字典序最小和最大的字符串,最長公共前綴即爲這兩個字符串的公共前綴
執行用時 :44 ms, 在所有 Python3 提交中擊敗了59.87%的用戶
內存消耗 :13.6 MB, 在所有 Python3 提交中擊敗了6.15%的用戶
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs: return ""
str0 = min(strs)
str1 = max(strs)
for i in range(len(str0)):
if str0[i] != str1[i]:
return str0[:i]
return str0
"""
For Example: input: strs = ["flower","flow","flight"]
output: "fl"
"""
strs = ["flower","flow","flight"]
solution = Solution()
result = solution.longestCommonPrefix(strs)
print('輸出爲:', result) # f1
參考
- https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/
- https://leetcode-cn.com/problems/longest-common-prefix/solution/zi-dian-xu-zui-da-he-zui-xiao-zi-fu-chuan-de-gong-/