leetcode 014 最长公共前缀(Longest Common Prefix) python 多种思路,最简代码

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''

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):
        """
        :type strs: List[str]
        :rtype: str
        """

        # Approach One: 一个笨方法
#         length = 0
#         leng = 10000
#         for s in strs :
#             if leng >= len(s):
#                 leng = len(s)
#                 mins = s

#         if len(strs)<1 or mins == "": return ""       # 考虑字符串列表或者字符串为空的特殊情况

#         for i in range( len(mins) ):
#             for j in range(len(strs)):
#                 print(strs[j][i],mins[i])
#                 if strs[j][i] == mins[i] and j == len(strs)-1:
#                     length += 1
#                     if len(mins)-1 == i :
#                         return mins[:length]
#                 if strs[j][i] != mins[i] :
#                     return mins[:length]


        # Approach Two: 排序法,代价不低:
        # if strs == []:
        #     return ''
        # lista = sorted(strs,key=len)  # 可以应用在所有可迭代对象的高阶函数sorted()简化思路一
        # str1=lista[0]
        # str_common = ''               # 直接用字符串存储返回值,然不是返回的整数位数,减少一部转化过程
        # for j in range(len(str1)):
        #     for i in range(1,len(lista)):    # 不用和自己比较,提高一点效率
        #         if lista[i][j] != str1[j]:   # 更简化的判断条件
        #             return str_common
        #     str_common = str_common + str1[j]
        # return str_common



        # Approach three 排序法更精简的写法,只需要比较第一个和最后一个的公共前缀:
        # if not strs:
        #     return ''
        # strs.sort()                        # sort()函数只用于列表的排序,字符串默认升序
        # print(strs)
        # res = ''
        # for i in range(len(strs[0])):
        #     if strs[-1][i] != strs[0][i]:   # 省略中间的比较,提高效率
        #         return res
        #     res += strs[0][i]
        # return res


        # Approach four
        if strs == []: return ''
        ans = ''
        for i, n in enumerate(strs[0]):
            for j in strs[1:]:
                if i >= len(j) or j[i] != n :
                    return ans
            ans += n
        return strs[0]
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值