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
}
相关推荐
o0o_-_7 小时前
【go/gopls/mcp】官方gopls内置mcp server使用
开发语言·后端·golang
_不会dp不改名_9 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌10 小时前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun10 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌10 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
爱编程的化学家12 小时前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
吃着火锅x唱着歌13 小时前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学13 小时前
【贪心算法】day10
c++·算法·leetcode·贪心算法
Tisfy14 小时前
LeetCode 0966.元音拼写检查器:三个哈希表实现
leetcode·字符串·散列表·题解·哈希表
ゞ 正在缓冲99%…15 小时前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找