func longestCommonPrefix(strs []string) string {
if len(strs) == 0 {
return ""
}
maxLength := len(strs[0])
strNumber := len(strs)
for i := 0; i < strNumber; i++ {
strLength := len(strs[i])
if strLength < maxLength {
if strLength == 0{
return ""
}
maxLength = strLength
}
}
i := 0
I:
for ; i < maxLength; i++ {
for j := 1; j < strNumber; j++ {
if strs[j][i] != strs[j-1][i] {
break I
}
}
}
return strs[0][0:i]
}
力扣14-最长公共前缀
最新推荐文章于 2022-11-25 14:39:29 发布