Contains Duplicate III Solutions in Go
Number 220
Difficulty Medium
Acceptance 20.9%
Link LeetCode
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport "sort"// 解法一 排序 + 滑动窗口func containsNearbyAlmostDuplicate(nums []int, k int, t int) bool {if len(nums) < 2 {return false}elemList := make([]*elem, len(nums))for i, num := range nums {elemList[i] = &elem{num, i}}sort.SliceStable(elemList, func(i, j int) bool {if elemList[i].val != elemList[j].val {return elemList[i].val < elemList[j].val}return elemList[i].idx < elemList[j].idx})i, j := 0, 1for j < len(elemList) {if elemList[j].val-elemList[i].val <= t {if abs(elemList[j].idx-elemList[i].idx) <= k {return true}j++} else {i++if j <= i {j++}}}return false}func abs(a int) int {if a > 0 {return a}return -a}type elem struct {val intidx int}// 解法二 滑动窗口 + 剪枝func containsNearbyAlmostDuplicate1(nums []int, k int, t int) bool {if len(nums) <= 1 {return false}if k <= 0 {return false}n := len(nums)for i := 0; i < n; i++ {count := 0for j := i + 1; j < n && count < k; j++ {if abs(nums[i]-nums[j]) <= t {return true}count++}}return false}