forked from ChienJuiLin/leetcode_heprecsler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_9.py
More file actions
72 lines (60 loc) · 1.73 KB
/
leetCode_9.py
File metadata and controls
72 lines (60 loc) · 1.73 KB
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
'''
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
'''
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
t=len(strs)
ans,i,corr,x = '',0,False,1
if t == 1: return strs[0]
elif t == 0 or '' in strs: return ''
minStringIndex = strs.index(min(strs, key=len))
strs[0],strs[minStringIndex] = strs[minStringIndex],strs[0]
while True:
newC = strs[0][i]
while x <= t-1:
if newC != strs[x][i]:
corr = True
break
x += 1
if corr: break
x = 1
ans = ans + newC
i += 1
if i >= len(strs[0]): break
return ans
# 這字串處理真的很漂亮
'''
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ("")
length = min(strs, key=len)
strs.remove(length)
'''
# 尤其是這個部分,直接把最短字的每個字母編號,再逐一和其他各String的各個字母比較
# 如果字母一不相等,就回傳前所有比對過的部分
'''
for i, x in enumerate(length):
for item in strs:
if item[i] != x:
return(length[:i])
return(length)
'''