forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_Longest Common Prefix.py
74 lines (66 loc) · 2.16 KB
/
14_Longest Common Prefix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 26 16:50:09 2019
@author: leiya
"""
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
res = ''
if strs == []:
return ''
#找到输入列表中最短的字符串的索引值,用这个最短的索引值的字符串作为搜索字符串
newstrs = list(map(len,strs))
min_index = newstrs.index(min(newstrs))
for i in range(len(strs[min_index])):
for j in strs:
if strs[min_index][i] != j[i]:
return res
res = res + strs[min_index][i]
return res
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
res = ''
if strs == []:
return ''
#找到输入列表中最短的字符串的索引值,用这个最短的索引值的字符串作为搜索字符串
newstrs = list(map(len,strs))
min_index = newstrs.index(min(newstrs))
for i in range(len(strs[min_index])):
for j in strs:
if j == min_index:
pass
else:
if strs[min_index][i] != j[i]:
return res
res = res + strs[min_index][i]
return res
class Solution:
def longestCommonPrefix(self, strs):
if not strs:
return ''
strs.sort(key=lambda x:len(x))
if strs[0] == '':
return ''
result = ""
for i in range(len(strs[0])):
sets = set(string[i] for string in strs)
if len(sets) == 1:
result += sets.pop()
else:
return result
return result
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
result = ""
i = 0
while True:
try:
sets = set(string[i] for string in strs)
if len(sets) == 1:
result += sets.pop()
i += 1
else:
break
except Exception as e:
break
return result