-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path14.最长公共前缀.py
63 lines (60 loc) · 1.34 KB
/
14.最长公共前缀.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# @lc app=leetcode.cn id=14 lang=python3
#
# [14] 最长公共前缀
#
# https://leetcode-cn.com/problems/longest-common-prefix/description/
#
# algorithms
# Easy (36.83%)
# Likes: 951
# Dislikes: 0
# Total Accepted: 223.2K
# Total Submissions: 605.7K
# Testcase Example: '["flower","flow","flight"]'
#
# 编写一个函数来查找字符串数组中的最长公共前缀。
#
# 如果不存在公共前缀,返回空字符串 ""。
#
# 示例 1:
#
# 输入: ["flower","flow","flight"]
# 输出: "fl"
#
#
# 示例 2:
#
# 输入: ["dog","racecar","car"]
# 输出: ""
# 解释: 输入不存在公共前缀。
#
#
# 说明:
#
# 所有输入只包含小写字母 a-z 。
#
#
from typing import List
# @lc code=start
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:return ''
_ = min(strs, key=len)
strs.remove(_)
for s in strs:
while s.find(_)!=0:
_ = _[:-1]
return _
# 找公共字符串可以用zip法if not strs:return ''
# res = ''
# for x in zip(*strs):
# if len(set(x)) == 1:
# res+=x[0]
# else:
# break
# return res
# @lc code=end
if __name__ == "__main__":
test = Solution()
print(test.longestCommonPrefix(["ca","a"]))