Skip to content

Latest commit

 

History

History
34 lines (32 loc) · 729 Bytes

存在重复元素.md

File metadata and controls

34 lines (32 loc) · 729 Bytes
/**
 * @param {number[]} nums
 * @param {number} k
 * @param {number} t
 * @return {boolean}

220. 存在重复元素 III
输入:nums = [1,5,9,1,5,9], k = 2, t = 3
输出:false

[1, 5] 9
 */
var containsNearbyAlmostDuplicate = function(nums, k, t) {
    let len = nums.length;
    let window = [];
    let ans = false;
    out: for(let i = 0; i < len; i++) {
        let value = nums[i]; // 9
        for(let j = 0; j < window.length; j++) {
            let w = window[j];
            if(Math.abs(w-value) <= t) {
                ans = true;
                break out;
            }
        }
        window.push(value);
        if(window.length > k) {
            window.shift();
        }
    }
    return ans;
};