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
}
相关推荐
_Itachi__6 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
roman_日积跬步-终至千里7 小时前
【Go语言基础【14】】defer与异常处理(panic、recover)
golang
孔令飞7 小时前
Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践
ai·云原生·容器·golang·kubernetes
chao_7898 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
编程绿豆侠10 小时前
力扣HOT100之多维动态规划:1143. 最长公共子序列
算法·leetcode·动态规划
我的golang之路果然有问题13 小时前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
孔令飞14 小时前
Go 为何天生适合云原生?
ai·云原生·容器·golang·kubernetes
YGGP17 小时前
吃透 Golang 基础:数据结构之 Map
开发语言·数据结构·golang
march of Time18 小时前
go工具库:hertz api框架 hertz client的使用
开发语言·golang·iphone