146 14 最長公共字首

2022-05-02 18:03:10 字數 3344 閱讀 9332

通過乙個函式來查詢陣列中所有的字串最長公共字首。(第乙個是我寫的,後面有幾個很經典的,看不懂請看力扣官網)

class solution(object):

def longestcommonprefix1(self, strs):

""":type strs: list[str]

:rtype: str

"""length = len(strs)

if not length:

return ""

i = 0

prepare_str = strs[0]

inner_length = len(min(strs, key=len))

while i < inner_length:

flag = true # 判斷是否發現了不一樣的字串

for j in range(1, length):

if strs[j][i] != prepare_str[i]:

flag = false

break

if not flag or i + 1 > inner_length:

break

i += 1

return prepare_str[:i]

def longestcommonprefix2(self, strs):

"""這個方法很好,先將和第二個字串相同的留下,每次會是的substr長度減少

:type strs: list[str]

:rtype: str

"""if len(strs) == 0:

return ""

strs = sorted(strs)

substr = strs[0]

for i in range(1, len(strs)):

while strs[i].find(substr) != 0:

substr = substr[:-1]

if substr == "":

return substr

return substr

def longestcommonprefix3(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)

def longestcommonprefix4(self, strs):

s=""

zip_str = zip(*strs)

for i in zip_str:

if len(set(i)) == 1:

s += i[0]

else:

return s

return s

def longestcommonprefix5(self, strs) -> 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]

def longestcommonprefix6(self, strs) -> str:

def lcp(start, end):

if start == end:

return strs[start]

mid = (start + end) // 2

inner_left, inner_right = lcp(start, mid), lcp(mid + 1, end)

min_len = min(len(inner_left), len(inner_right))

for i in range(min_len):

if inner_left[i] != inner_right[i]:

return inner_left[:i]

return inner_left[:min_len]

left, right = 0, len(strs) - 1

return "" if not strs else lcp(left, right)

def longestcommonprefix(self, strs) -> str:

"""二分法查詢還可以這樣用,很經典"""

def is_common_prefix(length):

str0, count = strs[0][:length], len(strs)

return all(strs[i][:length] == str0 for i in range(1, count))

if not strs:

return ""

min_length = min(len(s) for s in strs)

low, high = 0, min_length

while low < high:

mid = (high - low + 1) // 2 + low

if is_common_prefix(mid):

low = mid

else:

high = mid - 1

return strs[0][:low]

if __name__ == '__main__':

s = solution()

strs = ["flower", "flow", "flight"]

# strs = ["dog", "d","dacecar", "d"]

# strs = [""]

print(s.longestcommonprefix(strs))

最長公共字首

描述 給k個字串,求出他們的最長公共字首 lcp 樣例 在 abcd abef 和 acef 中,lcp 為 a 在 abcdefg abcefg abcefa 中,lcp 為 abc 新知識點 vectorstrs既可以是一維的,也可以是多維的。在這裡講解三維的初始化。vector str str...

最長公共字首

編寫乙個函式來查詢字串陣列中的最長公共字首。如果不存在公共字首,返回空字串 示例 1 輸入 flower flow flight 輸出 fl 示例 2 輸入 dog racecar car 輸出 解釋 輸入不存在公共字首。說明 所有輸入只包含小寫字母a z。class solution object...

最長公共字首

編寫乙個函式來查詢字串陣列中的最長公共字首。如果不存在公共字首,返回空字串 示例 1 輸入 flower flow flight 輸出 fl 示例 2 輸入 dog racecar car 輸出 解釋 輸入不存在公共字首。說明 所有輸入只包含小寫字母a z。param strs return var...