Write a function to find the longest common prefix string amongst an array of strings.
class Solution:
# @return a string
def longestCommonPrefix(self, strs):
minLength = 99999
shortest = ''
for i in strs:
if len(i) < minLength : minLength = len(i); shortest = i
prefix = ''
for i in range(0,len(shortest)):
char = shortest[i]
for j in range(0,len(strs)):
if strs[j][i] != char : return prefix
prefix += char
return prefix
本文介绍了一个函数,用于查找给定字符串数组中最长的公共前缀字符串。通过遍历最短字符串来实现这一目标,确保了算法效率。
315

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



