longest_consecutive

  • 2022-12-14
  • 浏览 (471)

longest_consecutive.go 源码

package leetcode

func longestConsecutive(nums []int) int {
	m := make(map[int]bool)
	for _, v := range nums {
		m[v] = true
	}
	res := 0
	for v := range m {
		if !m[v-1] {
			cur, curRes := v, 1
			for m[cur+1] {
				curRes++
				cur++
			}
			if curRes > res {
				res = curRes
			}
		}
	}
	return res
}

你可能感兴趣的文章

array_intersection

array_intersection_ii

find_all_anagrams

0  赞