mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-19 21:31:22 +08:00
24 lines
470 B
Go
24 lines
470 B
Go
package leetcode
|
|
|
|
import "sort"
|
|
|
|
func longestCommonPrefix(strs []string) string {
|
|
sort.Slice(strs, func(i, j int) bool {
|
|
return len(strs[i]) <= len(strs[j])
|
|
})
|
|
minLen := len(strs[0])
|
|
if minLen == 0 {
|
|
return ""
|
|
}
|
|
var commonPrefix []byte
|
|
for i := 0; i < minLen; i++ {
|
|
for j := 1; j < len(strs); j++ {
|
|
if strs[j][i] != strs[0][i] {
|
|
return string(commonPrefix)
|
|
}
|
|
}
|
|
commonPrefix = append(commonPrefix, strs[0][i])
|
|
}
|
|
return string(commonPrefix)
|
|
}
|