首页 > 代码库 > Longest Common Prefix
Longest Common Prefix
class Solution: # @return a string def longestCommonPrefix(self, strs): num_items=len(strs) if num_items<1: return "" prefix=strs[0] for i in strs[1:num_items]: if len(prefix)>len(i): prefix=prefix[0:len(i)] min_len=min(len(i),len(prefix)) for j in range(min_len): if prefix[j]!=i[j]: prefix=prefix[0:j] break return prefixif __name__ == ‘__main__‘: s=Solution() #strs=["abd","abcd","abcf","abchrg"] strs=["flower","flow","flight"] print(s.longestCommonPrefix(strs))
Longest Common Prefix
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。