LeetCode No.14 | StriveZs的博客

LeetCode No.14

LeetCode第十四题

题目描述

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
示例 1

输入:strs = ["flower","flow","flight"]
输出:"fl"
示例 2

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

提示:

0 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成

代码

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
import re

class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
if strs[0] == '':
return ""
commanForward = ''
flag = False
index = -1
for i in range(len(strs[0])):
commanForward += strs[0][i]
j = 1
while j < len(strs):
if commanForward != strs[j][0:i+1]:
index = i
flag = True
break
j += 1
if flag:
break
if ~flag:
index = i+1
if index == -1:
return ""
else:
return strs[0][0:index]

if __name__ == '__main__':
s = Solution()
print(s.longestCommonPrefix(["a","a","b"]))
StriveZs wechat
Hobby lead  creation, technology change world.