[勇者闯LeetCode] 14. Longest Common Prefix
Description
Write a function to find the longest common prefix string amongst an array of strings.
Tags: StringDifficulty: Easy
Solution
列向扫描字符是否相同,直到字符不相同或common prefix的长度等于某个字符串的长度。
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]
for i
in range(len(strs[
0])):
for j
in range(
1, len(strs)):
if i == len(strs[j])
or strs[
0][i] != strs[j][i]:
return strs[
0][:i]
return strs[
0]
转载请注明原文地址: https://ju.6miu.com/read-25995.html