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
}
相关推荐
样例过了就是过了19 分钟前
LeetCode热题100 爬楼梯
c++·算法·leetcode·动态规划
ThisIsMirror34 分钟前
leetcode 452 Arrays.sort()排序整数溢出、Integer.compare(a[1], b[1])成功的问题
算法·leetcode
x_xbx2 小时前
LeetCode:438. 找到字符串中所有字母异位词
算法·leetcode·职场和发展
m0_694845572 小时前
UVdesk部署教程:企业级帮助台系统实践
服务器·开发语言·后端·golang·github
@atweiwei2 小时前
Go语言面试篇数据结构底层原理精讲(下)
数据结构·面试·golang
派大星~课堂2 小时前
【力扣-94.二叉树的中序遍历】Python笔记
笔记·python·leetcode
XMYX-03 小时前
03 - Go 常用类型速查表 + 实战建议(实战向)
开发语言·golang
AI成长日志3 小时前
【笔面试算法学习专栏】堆与优先队列实战:力扣hot100之215.数组中的第K个最大元素、347.前K个高频元素
学习·算法·leetcode
6Hzlia3 小时前
【Hot 100 刷题计划】 LeetCode 45. 跳跃游戏 II | C++ 贪心算法最优解题解
c++·leetcode·游戏
北顾笙9803 小时前
day18-数据结构力扣
数据结构·算法·leetcode