Golang | Leetcode Golang题解之第128题最长连续序列

题目:

题解:

Go 复制代码
func longestConsecutive(nums []int) int {
    numSet := map[int]bool{}
    for _, num := range nums {
        numSet[num] = true
    }
    longestStreak := 0
    for num := range numSet {
        if !numSet[num-1] {
            currentNum := num
            currentStreak := 1
            for numSet[currentNum+1] {
                currentNum++
                currentStreak++
            }
            if longestStreak < currentStreak {
                longestStreak = currentStreak
            }
        }
    }
    return longestStreak
}
相关推荐
Navigator_Z20 小时前
LeetCode //C - 1328. Break a Palindrome
c语言·算法·leetcode
NendoZen20 小时前
Bubble Tea V2构建Go TUI应用教程
开发语言·golang·xcode
pan_junbiao21 小时前
Go 语言变量的声明与初始化
开发语言·javascript·golang
pan_junbiao1 天前
Go 语言基本数据类型
java·前端·golang
codeejun2 天前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
利威尔xu2 天前
Go context取消传播3层:信号传不到第3层goroutine,泄漏拖垮
后端·golang
Sakura_j_h_2 天前
TypeScript 7.0 换 Go 编译器后,我先在 6.0 清完默认值
javascript·golang·typescript·vite·前端工程化·tsc
旖旎夜光3 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
木井巳3 天前
【动态规划: 子序列问题】最长递增子序列的个数
java·算法·leetcode·动态规划·推荐算法
码途行者3 天前
每天10分钟学Go:第23课 协程(goroutine)
开发语言·后端·golang