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
}
相关推荐
mit6.82429 分钟前
论容器化 | 分析Go和Rust做医疗的后端服务
docker·golang·rust
ykuaile_h81 小时前
Go 编译报错排查:vendor/golang.org/x/crypto/cryptobyte/asn1 no Go source files
后端·golang
科大饭桶1 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
YuTaoShao3 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
前端拿破轮5 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试
凤年徐5 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
李昊_5 小时前
【LeetCode 3440. 重新安排会议得到最多空余时间 II】解析
算法·leetcode
呆呆的小鳄鱼6 小时前
leetcode:322. 零钱兑换[完全背包]
算法·leetcode·职场和发展
এ᭄画画的北北6 小时前
力扣-240.搜索二维矩阵 II
算法·leetcode·矩阵
JJ1M87 小时前
前缀和+贪心总结,基于每日一题力扣3439、3440
python·算法·leetcode