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
}
相关推荐
幼稚园的山代王1 天前
go语言了解
开发语言·后端·golang
zhuyasen1 天前
踩坑实录:Go 1.25.x 编译的 exe 在 Windows 提示“此应用无法运行”
windows·golang
Lris-KK1 天前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
坚持编程的菜鸟1 天前
LeetCode每日一题——螺旋矩阵
c语言·算法·leetcode·矩阵
(●—●)橘子……1 天前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
GalaxyPokemon1 天前
LeetCode - 1171.
算法·leetcode·链表
iナナ1 天前
Java优选算法——位运算
java·数据结构·算法·leetcode
Han.miracle1 天前
数据结构二叉树——层序遍历&& 扩展二叉树的左视图
java·数据结构·算法·leetcode
L_09071 天前
【Algorithm】Day-4
c++·算法·leetcode
代码充电宝1 天前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展