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
}
相关推荐
迷藏4941 天前
**超融合架构下的Go语言实践:从零搭建高性能容器化微服务集群**在现代云原生时代,*
java·python·云原生·架构·golang
x_xbx1 天前
LeetCode:42. 接雨水
算法·leetcode·职场和发展
geovindu1 天前
go: Model,Interface,DAL ,Factory,BLL using mysql
开发语言·mysql·设计模式·golang·软件构建
sheeta19981 天前
LeetCode 每日一题笔记 日期:2026.04.14 题目:2463.最小移动距离
笔记·算法·leetcode
剑挑星河月1 天前
55.跳跃游戏
数据结构·算法·leetcode
小辉同志1 天前
208. 实现 Trie (前缀树)
开发语言·c++·leetcode·图论
py有趣1 天前
力扣热门100题之括号生成
算法·leetcode
hanbr1 天前
每日一题day2(Leetcode 704二分查找)
数据结构·算法·leetcode
语戚1 天前
力扣 2463. 最小移动总距离 —— 动态规划 & 贪心排序全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·dp