Golang | Leetcode Golang题解之第220题存在重复元素III

题目:

题解:

Go 复制代码
func getID(x, w int) int {
    if x >= 0 {
        return x / w
    }
    return (x+1)/w - 1
}

func containsNearbyAlmostDuplicate(nums []int, k, t int) bool {
    mp := map[int]int{}
    for i, x := range nums {
        id := getID(x, t+1)
        if _, has := mp[id]; has {
            return true
        }
        if y, has := mp[id-1]; has && abs(x-y) <= t {
            return true
        }
        if y, has := mp[id+1]; has && abs(x-y) <= t {
            return true
        }
        mp[id] = x
        if i >= k {
            delete(mp, getID(nums[i-k], t+1))
        }
    }
    return false
}

func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
相关推荐
XWalnut27 分钟前
LeetCode刷题 day13
数据结构·算法·leetcode
codeejun1 小时前
每日一Go-53、Go微服务--限流与降级
开发语言·微服务·golang
NotFound4861 小时前
Go语言中的图形界面开发实战解析:从GUI到WebAssembly
开发语言·golang·wasm
2401_832635582 小时前
小白分享如何Go 语言中的图形界面开发:从 GUI 到 WebAssembly
microsoft·golang·wasm
im_AMBER2 小时前
Leetcode 158 数组中的第K个最大元素 | 查找和最小的 K 对数字
javascript·数据结构·算法·leetcode·
脱氧核糖核酸__2 小时前
LeetCode热题100——48.旋转图像(题解+答案+要点)
c++·算法·leetcode
木井巳2 小时前
【递归算法】字母大小写全排列
java·算法·leetcode·决策树·深度优先
Mr_pyx2 小时前
LeetCode HOT 100 —— 矩阵置零(多种解法详解)
算法·leetcode·矩阵
Q741_1472 小时前
每日一题 力扣 3761. 镜像对之间最小绝对距离 哈希表 数组 C++ 题解
c++·算法·leetcode·哈希算法·散列表
我真不是小鱼2 小时前
cpp刷题打卡记录29——矩阵置零 & 旋转图像 & 除了自身以外数组的乘积
数据结构·c++·算法·leetcode·矩阵