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
}
相关推荐
小poop1 小时前
轮转数组:从暴力到最优,一题掌握算法复杂度分析
数据结构·算法·leetcode
Wang's Blog4 小时前
Go-Zero项目开发40: 基于Jaeger的分布式链路跟踪实战
开发语言·分布式·golang
TDengine (老段)4 小时前
TDengine Go 与 Rust 连接器 — 高性能异步访问
大数据·数据库·物联网·golang·rust·时序数据库·tdengine
玖玥拾4 小时前
LeetCode 27 移除元素
算法·leetcode
hanlin036 小时前
刷题笔记:力扣第704、977、209题(数组相关)
笔记·算法·leetcode
Wang's Blog7 小时前
Go-Zero项目开发38:深入限流器实现与应用
开发语言·golang·go-zero
Rabitebla7 小时前
C++ 内存管理全面复习:从内存分布到 operator new/delete
java·c语言·开发语言·c++·算法·leetcode
xcLeigh7 小时前
Go入门:main包与main函数的特殊地位
开发语言·后端·golang
玖玥拾7 小时前
LeetCode 58 最后一个单词的长度
算法·leetcode
hanlin038 小时前
刷题笔记:力扣第19题-删除链表的倒数第N个结点
笔记·leetcode·链表