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
}
相关推荐
GoFly开发者1 小时前
GoFly快速开发框架/Go语言封装的图像相似性比较插件使用说明
开发语言·后端·golang
子非鱼9211 小时前
【JavaScript】LeetCode:41-45
开发语言·javascript·leetcode·链表·二叉树
翔云1234561 小时前
Go语言的垃圾回收(GC)机制的迭代和优化历史
java·jvm·golang·gc
@听风吟2 小时前
力扣之182.查找重复的电子邮箱
大数据·javascript·数据库·sql·leetcode
__AtYou__4 小时前
Golang | Leetcode Golang题解之第417题太平洋大西洋水流问题
leetcode·golang·题解
拉玛干4 小时前
社团周报系统可行性研究-web后端框架对比-springboot,django,gin
数据库·python·spring·golang
Ddddddd_1584 小时前
C++ | Leetcode C++题解之第421题数组中两个数的最大异或值
c++·leetcode·题解
ly-how4 小时前
leetcode练习 二叉树的层序遍历
算法·leetcode
大二转专业4 小时前
408算法题leetcode--第10天
考研·算法·leetcode
shark-chili5 小时前
数据结构与算法-Trie树添加与搜索
java·数据结构·算法·leetcode