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%
最长公共前缀算法解析
本文介绍了一种求解字符串数组中最长公共前缀的方法,通过一个Python实现的示例详细解释了算法的工作原理及步骤。该算法适用于寻找一组字符串中的最长相同起始部分。
282

被折叠的 条评论
为什么被折叠?



