Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question
思路简单,首先找到最短的那个,然后把它,一位一位减少,跟每个字符串比较,看是不是前缀
class Solution(object):
def longestCommonPrefix(self, strs):
if strs == None or strs == []:
return ''
minlen = 99999
for i in range(0,len(strs)):
if minlen > len(strs[i]):
minlen = len(strs[i])
pos = i
#print minlen,pos
minstr = strs[pos]
t = ''
for i in range(minlen-1,-1,-1):
t = minstr[0:i+1]
#print t
flag = 1
for j in strs:
if t != j[0:i+1]:
flag = 0
break
if flag == 1:
return t
return ''