Skip to content

Commit 79ee689

Browse files
committed
最长公共前缀
1 parent e4f6ffd commit 79ee689

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

longest_common_prefix.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param strs: A list of strings
5+
# @return: The longest common prefix
6+
def longestCommonPrefix(self, strs):
7+
# write your code here
8+
ret = []
9+
i = 0
10+
while True:
11+
ch = None
12+
for _str in strs:
13+
if i >= len(_str):
14+
ch = None
15+
break
16+
if not ch:
17+
ch = _str[i]
18+
else:
19+
if ch != _str[i]:
20+
ch = None
21+
break
22+
if ch:
23+
ret.append(ch)
24+
i += 1
25+
else:
26+
break
27+
return ''.join(ret)

0 commit comments

Comments
 (0)