首页 > 代码库 > 14. Longest Common Prefix

14. Longest Common Prefix

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]不要超出范围

14. Longest Common Prefix