题目:

 

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

 

解答:

 

方法一:

class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        result = ''
        for each in zip(*strs):
            if len(set(each)) == 1:
                result += each[0]
            else:
                break
        return result

  

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2021-12-21
  • 2021-06-03
  • 2021-10-27
  • 2021-04-22
  • 2021-09-12
猜你喜欢
  • 2020-01-01
  • 2021-06-30
  • 2021-04-02
  • 2021-06-01
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案