leetcode上有一道最长公共前缀的问题,想换一种方式解,然而打出来的是:
fl
fl
flo
为何print会出来3个结果?
class Solution:
def inter_prefix(self,strs=list,minPrefix=str):
if minPrefix == '': return minPrefix
for i in range(len(strs)):
mi = strs[i][:len(minPrefix)]
if minPrefix != mi:
minPrefix = minPrefix[:-1]
self.inter_prefix(strs, minPrefix)
print(minPrefix)
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs: return ''
minPrefix = strs[0]
# if len(strs) == 1:return (minPrefix)
for i in range(len(strs)):
if len(minPrefix) > len(strs[i]):
minPrefix = strs[i]
self.inter_prefix(strs,minPrefix)
if name == '__main__':
Solution().longestCommonPrefix(["flower","flow","flight"])
慕莱坞森
相关分类