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
}
相关推荐
sprintzer8 小时前
2.06-2.15力扣数学刷题
算法·leetcode·职场和发展
滴滴答滴答答10 小时前
LeetCode Hot100 之 17 有效的括号
算法·leetcode·职场和发展
老鼠只爱大米10 小时前
LeetCode经典算法面试题 #20:有效的括号(数组模拟法、递归消除法等五种实现方案详细解析)
算法·leetcode··括号匹配·数组模拟法·递归消除法
不想看见40410 小时前
6.3Permutations -- 回溯法--力扣101算法题解笔记
笔记·算法·leetcode
wangwangmoon_light13 小时前
1.2 LeetCode总结(线性表)_双指针
算法·leetcode·职场和发展
qinaoaini14 小时前
[golang][MAC]Go环境搭建+VsCode配置
vscode·macos·golang
Bear on Toilet14 小时前
BFS_FloodFill_46 . 腐烂的橘子问题
数据结构·c++·算法·leetcode·宽度优先
样例过了就是过了14 小时前
LeetCode热题100 找到字符串中所有字母异位词
算法·leetcode
花酒锄作田14 小时前
Go - slog使用入门
golang
u***357414 小时前
对基因列表中批量的基因进行GO和KEGG注释
开发语言·数据库·golang