python学习日志6

    xiaoxiao2021-08-22  117

    leetcode之Longest Common Prefix解法

    class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if not strs: return '' min=2147483648 for n in strs: if len(n)<min: min=len(n) if min==0: return '' l=[] i=0 while i<min: j=0 while j<len(strs)-1: if strs[j][i]!=strs[j+1][i]: return ''.join(l) j=j+1 l.insert(len(l),strs[j][i]) i=i+1 return ''.join(l)

    学习总结:

    1.list为空不能用None,因为None是一个特殊的常量,None和任何其他的数据类型比较永远返回False

    2.python字符串==是值比较

    更多leetcode解题源码,请查看我的github地址https://github.com/Jum1023/leetcode

    转载请注明原文地址: https://ju.6miu.com/read-676857.html

    最新回复(0)