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
}
相关推荐
microrain7 小时前
先应答,再入库:SagooIoT 接入 GB/T 32960 的四层宿主改造
物联网·golang·开源·sagooiot
夏幻灵11 小时前
每日一题:LeetCode 3:无重复字符的最长子串——滑动窗口思路详解
算法·leetcode·职场和发展
无忧.芙桃12 小时前
C++ 并查集详解:原理、实现与应用
c语言·c++·算法·leetcode
蛋蛋的就会顺顺的12 小时前
hot100——二叉树
java·数据结构·算法·leetcode·力扣
Nil20815 小时前
leetcode 55跳跃游戏
算法·leetcode·游戏
longlongzihan15 小时前
LeetCode 128. 最长连续序列 —— 从排序到哈希集合
数据结构·c++·算法·leetcode·排序算法·哈希表
谢亮_vipxieliang16 小时前
Go defer、panic与recover核心知识点
开发语言·后端·golang
谢亮_vipxieliang17 小时前
Go基础数据类型全解
开发语言·后端·golang
圣保罗的大教堂17 小时前
leetcode 3302. 字典序最小的合法序列 中等
leetcode
谢亮_vipxieliang17 小时前
Go 接口设计原则核心知识点
开发语言·ios·golang