mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
Updated README
This commit is contained in:
@ -40,25 +40,18 @@ If there is no common prefix, return an empty string "".
|
|||||||
|
|
||||||
package leetcode
|
package leetcode
|
||||||
|
|
||||||
import "sort"
|
|
||||||
|
|
||||||
func longestCommonPrefix(strs []string) string {
|
func longestCommonPrefix(strs []string) string {
|
||||||
sort.Slice(strs, func(i, j int) bool {
|
prefix := strs[0]
|
||||||
return len(strs[i]) <= len(strs[j])
|
|
||||||
})
|
for i := 1; i < len(strs); i++ {
|
||||||
minLen := len(strs[0])
|
for j := 0; j < len(prefix); j++ {
|
||||||
if minLen == 0 {
|
if len(strs[i]) <= j || strs[i][j] != prefix[j] {
|
||||||
return ""
|
prefix = prefix[0:j]
|
||||||
}
|
break
|
||||||
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)
|
|
||||||
|
return prefix
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user