Longest Consecutive Sequence Solutions in Go
Number 128
Difficulty Hard
Acceptance 45.2%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("github.com/halfrost/LeetCode-Go/template")// 解法一 map,时间复杂度 O(n)func longestConsecutive(nums []int) int {res, numMap := 0, map[int]int{}for _, num := range nums {if numMap[num] == 0 {left, right, sum := 0, 0, 0if numMap[num-1] > 0 {left = numMap[num-1]} else {left = 0}if numMap[num+1] > 0 {right = numMap[num+1]} else {right = 0}// sum: length of the sequence n is insum = left + right + 1numMap[num] = sum// keep track of the max lengthres = max(res, sum)// extend the length to the boundary(s) of the sequence// will do nothing if n has no neighborsnumMap[num-left] = sumnumMap[num+right] = sum} else {continue}}return res}func max(a int, b int) int {if a > b {return a}return b}// 解法二 并查集func longestConsecutive1(nums []int) int {if len(nums) == 0 {return 0}numMap, countMap, lcs, uf := map[int]int{}, map[int]int{}, 0, template.UnionFind{}uf.Init(len(nums))for i := 0; i < len(nums); i++ {countMap[i] = 1}for i := 0; i < len(nums); i++ {if _, ok := numMap[nums[i]]; ok {continue}numMap[nums[i]] = iif _, ok := numMap[nums[i]+1]; ok {uf.Union(i, numMap[nums[i]+1])}if _, ok := numMap[nums[i]-1]; ok {uf.Union(i, numMap[nums[i]-1])}}for key := range countMap {parent := uf.Find(key)if parent != key {countMap[parent]++}if countMap[parent] > lcs {lcs = countMap[parent]}}return lcs}// 解法三 暴力解法,时间复杂度 O(n^2)func longestConsecutive2(nums []int) int {if len(nums) == 0 {return 0}numMap, length, tmp, lcs := map[int]bool{}, 0, 0, 0for i := 0; i < len(nums); i++ {numMap[nums[i]] = true}for key := range numMap {if !numMap[key-1] && !numMap[key+1] {delete(numMap, key)}}if len(numMap) == 0 {return 1}for key := range numMap {if !numMap[key-1] && numMap[key+1] {length, tmp = 1, key+1for numMap[tmp] {length++tmp++}lcs = max(lcs, length)}}return max(lcs, length)}