init
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package top100liked
|
||||
|
||||
import "testing"
|
||||
|
||||
// https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func longestConsecutive(nums []int) int {
|
||||
numSet := map[int]bool{}
|
||||
for _, num := range nums {
|
||||
numSet[num] = true
|
||||
}
|
||||
res := 0
|
||||
for k := range numSet {
|
||||
if numSet[k-1] {
|
||||
continue
|
||||
}
|
||||
curNum := k
|
||||
curLen := 1
|
||||
for numSet[curNum+1] {
|
||||
curNum++
|
||||
curLen++
|
||||
}
|
||||
if curLen > res {
|
||||
res = curLen
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func TestS3(t *testing.T) {
|
||||
println(longestConsecutive([]int{100, 4, 200, 1, 3, 2}))
|
||||
}
|
||||
Reference in New Issue
Block a user