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
}
相关推荐
七七&5563 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤3 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油3 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
恋喵大鲤鱼6 小时前
Golang 后台技术面试套题 1
面试·golang
Swift社区6 小时前
Swift 实战:实现一个简化版的 Twitter(LeetCode 355)
leetcode·swift·twitter
火车叨位去194912 小时前
力扣top100(day04-05)--堆
算法·leetcode·职场和发展
Miraitowa_cheems13 小时前
LeetCode算法日记 - Day 11: 寻找峰值、山脉数组的峰顶索引
java·算法·leetcode
楼田莉子14 小时前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
姜不吃葱17 小时前
【力扣热题100】双指针—— 接雨水
数据结构·算法·leetcode·力扣热题100
zzx_blog17 小时前
简单易懂的leetcode 100题-第三篇 移动0,颜色分类,数组中的第K个最大元素
leetcode·面试