32 lines
482 B
Go
32 lines
482 B
Go
package leetcode
|
|
|
|
func longestCommonPrefix(strs []string) string {
|
|
if len(strs) == 0 {
|
|
return ""
|
|
}
|
|
if len(strs) == 1 {
|
|
return strs[0]
|
|
}
|
|
var index int
|
|
first := strs[0]
|
|
secList := strs[1:]
|
|
max := len(first)
|
|
for _, str := range secList {
|
|
l := len(str)
|
|
if l < max {
|
|
max = l
|
|
}
|
|
}
|
|
OUTLOOP:
|
|
for i := 0; i < max; i++ {
|
|
c := first[i : i+1]
|
|
for _, str := range secList {
|
|
if str[i:i+1] != c {
|
|
break OUTLOOP
|
|
}
|
|
}
|
|
index = i + 1
|
|
}
|
|
return first[:index]
|
|
}
|