-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14. Longest Common Prefix.py
43 lines (41 loc) · 1.38 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
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
result = ""
i = 0
while True:
try:#输进去的东西是string的以及string 的各个字符
sets = set(string[i] for string in strs)#sets 是一种数据类型,叫做集合
print('sets is ',sets)#i 递增,然后去每一位的检验有没有相同的东西
# print(sets.pop())#pop这个函数删除了set结构的某些东西
# print('ok')
if len(sets) == 1 :
result += sets.pop()#删除列表中的一个数值,默认是最后一个
print(result)
i+=1
else:
break
except Exception as e:
print(e)
break
return result
strs = ["flower","flow","flight"]
so = Solution()
print(so.longestCommonPrefix(strs))
# #找出字符串中的重复的字符
# str='akwdlkandwm,nam,wndajbddaklwnldhawlhdlahndwla'
# dupstr=set()
# for i in str:
# if i in str:
# dupstr.add(i)
#
# print (dupstr)
# for i in range(0,3):
# for j in strs[i]:
# if j in strs[1] and j in strs[2] and j in strs[0]:
# print(j)
# else:
# pass