题目

Write a function to find the longest common prefix string amongst an array of strings.

Python

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def longestCommonPrefix(self, strs):
if strs == []:
return ""
f = strs[0]
for i in strs[1:]:
r = 0
for idx in range(0, min(len(f), len(i))):
if f[:idx+1] == i[:idx+1]:
r += 1
f = f[:r]
return f