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
}
相关推荐
踩坑记录6 小时前
leetcode hot100 2.两数相加 链表 medium
leetcode·链表
历程里程碑9 小时前
滑动窗口---- 无重复字符的最长子串
java·数据结构·c++·python·算法·leetcode·django
TracyCoder12310 小时前
LeetCode Hot100(18/100)——160. 相交链表
算法·leetcode
放荡不羁的野指针11 小时前
leetcode150题-滑动窗口
数据结构·算法·leetcode
女王大人万岁12 小时前
Go标准库 io与os库详解
服务器·开发语言·后端·golang
TracyCoder12312 小时前
LeetCode Hot100(13/100)——238. 除了自身以外数组的乘积
算法·leetcode
Anastasiozzzz12 小时前
LeetCode Hot100 215. 数组中的第K个最大元素
数据结构·算法·leetcode
让我上个超影吧12 小时前
【力扣76】最小覆盖子串
算法·leetcode·职场和发展
求梦82014 小时前
【力扣hot100题】合并两个有序链表(22)
算法·leetcode·链表
女王大人万岁14 小时前
Go语言time库核心用法与实战避坑
服务器·开发语言·后端·golang