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
}
相关推荐
_不会dp不改名_9 小时前
leetcode_3010 将数组分成最小总代价的子数组 I
算法·leetcode·职场和发展
Kx_Triumphs12 小时前
计算几何-旋转卡壳两种实现方案(兼P1452题解
算法·题解
lingggggaaaa14 小时前
安全工具篇&Go魔改二开&Fscan扫描&FRP代理&特征消除&新增扩展&打乱HASH
学习·安全·web安全·网络安全·golang·哈希算法
Tisfy14 小时前
LeetCode 3637.三段式数组 I:一次遍历(三种实现)
算法·leetcode·题解·模拟·数组·遍历·moines
期末考复习中,蓝桥杯都没时间学了14 小时前
力扣刷题15
算法·leetcode·职场和发展
£漫步 云端彡15 小时前
Golang学习历程【第十篇 方法(method)与接收者】
开发语言·学习·golang
im_AMBER15 小时前
Leetcode 111 两数相加
javascript·笔记·学习·算法·leetcode
TracyCoder12315 小时前
LeetCode Hot100(21/100)——234. 回文链表
算法·leetcode·链表
@––––––15 小时前
力扣hot100—系列1
算法·leetcode·职场和发展
老鼠只爱大米15 小时前
LeetCode经典算法面试题 #236:二叉树的最近公共祖先(RMQ转化、Tarjan离线算法等五种实现方案详细解析)
算法·leetcode·二叉树·lca·并查集·最近公共祖先·rmq