您的位置:首页 > 编程语言 > Go语言

[勇者闯LeetCode] 14. Longest Common Prefix

2017-03-10 23:28 344 查看

[勇者闯LeetCode] 14. Longest Common Prefix

Description

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

Information

Tags: String

Difficulty: 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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm leetcode