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

 

 1 class Solution(object):
 2     def longestCommonPrefix(self, strs):
 3         """
 4         :type strs: List[str]
 5         :rtype: str
 6         """
 7         if strs==[]:
 8             return ''
 9 
10         res = ''
11         for j in xrange (0,len(strs[0])):
12             for i in xrange (1,len(strs)):
13                 if j > len(strs[i])-1 or strs[0][j] != strs[i][j]:
14                     return res
15             res = res+strs[0][j]
16         return res
17         

注意strs[i][j]不要超出范围

posted on 2017-03-02 10:02  Ci_pea  阅读(105)  评论(0编辑  收藏  举报