• 为了保证你在浏览本网站时有着更好的体验,建议使用类似Chrome、Firefox之类的浏览器~~
    • 如果你喜欢本站的内容何不Ctrl+D收藏一下呢,与大家一起分享各种编程知识~
    • 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此,生命在于折腾,何不年轻时多折腾一下

14. Longest Common Prefix

leetcode admin 1周前 (02-11) 32次浏览 0个评论 扫描二维码

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

解法:
class Solution:
    def longestCommonPrefix(self, strs: 'List[str]') -> 'str':
        if len(strs)<1:
            return ''
        min_length=min(map(lambda x:len(x),strs))
        if min_length<1:
            return ''
        result=''
        for x in range(min_length):
            tmp=set([ data[x] for data in strs])
            if len(tmp)==1:
                result+=strs[0][x]
            else:
                return result

Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明14. Longest Common Prefix
喜欢 (0)
admin
关于作者:

您必须 登录 才能发表评论!