您的位置:首页 > 其它

14. Longest Common Prefix

2016-07-26 19:34 453 查看

14. Longest Common Prefix

Leetcode link for this question

Discription:

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

Analyze:

Code 1:

class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
if not strs[0]:
return ""
l=0
tmp=strs[0][l]
while 1:
for i in strs:
if l>=len(i):
return strs[0][0:l]
if i[l] !=tmp:
return strs[0][0:l]
l+=1
if l>=len(strs[0]):
return strs[0][0:l]
tmp=strs[0][l]
return strs[0][0:l+1]


Submission Result:

Status: Accepted

Runtime: 52 ms

Ranking: beats 73.43%
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: