思路:
直接比较最小和最大两个字符串即可。
而获取最小最大可以直接使用min max函数。
# 编写一个函数来查找字符串数组中的最长公共前缀。## 如果不存在公共前缀,返回空字符串 ""。## 示例 1:## 输入: ["flower","flow","flight"]# 输出: "fl"### 示例 2:## 输入: ["dog","racecar","car"]# 输出: ""# 解释: 输入不存在公共前缀。### 说明:## 所有输入只包含小写字母 a-z 。# Related Topics 字符串# 👍 1218 👎 0# leetcode submit region begin(Prohibit modification and deletion)class Solution(object):def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""pre = ''if len(strs) == 0:return premin_str = min(strs)max_str = max(strs)for index, item in enumerate(min_str):if max_str[index] != min_str[index]:return min_str[:index]return min_str# leetcode submit region end(Prohibit modification and deletion)s = Solution()print(s.longestCommonPrefix(["flower", "flow", "flight"]))print(s.longestCommonPrefix(["c", "c"]))print(s.longestCommonPrefix(["a", "b"]))
