leetcode-14题:longest common prefix

题目:

Write a function to find the longest common prefix string amongst an array of strings.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if strs==None or len(strs)==0:
return ''
res = strs[0]
for str in strs:
i = 0
while i<min(len(res),len(str)) and res[i]==str[i]:
i += 1
if i == 0:
return ''
else:
res = res[:i]
return res