[LintCode]Longest Common Prefix(Python)

xiaoxiao2021-02-28  62

class Solution: # @param strs: A list of strings # @return: The longest common prefix def longestCommonPrefix(self, strs): # write your code here minlen = 99999 for str in strs: if len(str) < minlen: minlen = len(str) if len(strs) == 0: minlen = 0 for i in range(minlen)[::-1]: prefixstrs = [] for str in strs: prefixstrs.append(str[:i + 1]) flag = True for str in prefixstrs: if str != prefixstrs[0]: flag = False if flag: return prefixstrs[0] return ''
转载请注明原文地址: https://www.6miu.com/read-33581.html

最新回复(0)