128. 最长连续序列

128. 最长连续序列

看了题解:

func longestConsecutive(nums []int) int {
    nMap := map[int]bool{}

    for _, n := range nums {
        nMap[n]=true
    }

    longest := 0
    for n := range nMap {
        if nMap[n-1] {
            continue
        }

        cur := 0
        for nMap[n] {
            n++
            cur++
        }

        longest = max(longest, cur)
    }

    return longest
}

https://leetcode.cn/problems/longest-consecutive-sequence/solutions/276931/zui-chang-lian-xu-xu-lie-by-leetcode-solution

这个大哥的评论比较好理解: Pasted image 20241026175401.png


本站总访问量次 本站访客数人次 本文总阅读量