[LeetCode] Contains Duplicate II 包含重复值之二

Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.

这道题是之前那道Contains Duplicate 包含重复值的延伸,不同之处在于那道题只要我们判断下数组中是否有重复值,而这道题限制了数组中只许有一组重复的数字,而且他们坐标差不能超过k。那么我们首先需要一个哈希表,来记录每个数字和其坐标的映射,然后我们需要一个变量d来记录第一次出现重复数字的坐标差。由于题目要求只能有一组重复的数字,所以我们在遇到重复数字时,首先判断d是否已经存了值,如果d已经有值了,说明之前有过了重复数字,则直接返回false即可。如果没有,则此时给d附上值。在网上看到有些解法在这里就直接判断d和k的关系然后返回结果了,其实这样是不对的。因为题目要求只能有一组重复数,就是说如果后面又出现了重复数,就没法继续判断了。所以正确的做法应该是扫描完整个数组后在判断,先看d有没有存入结果,如果没有,则说明没出现过重复数, 返回false即可。如果d有值,再跟k比较,返回对应的结果。OJ的test case没有包含所有的情况,比如当nums = [1, 2, 3, 1, 3], k = 3时,实际上应该返回false,但是有些返回true的算法也能通过OJ。个人认为正确的解法应该入下:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> m;
        int d = 0;
        for (int i = 0; i < nums.size(); ++i) {
            if (m.find(nums[i]) != m.end()) {
                if (d > 0) return false;
                d = i - m[nums[i]];
            }
            m[nums[i]] = i;
        }
        return d == 0 ? false : d <= k;
    }
};
时间: 2024-12-26 01:49:43

[LeetCode] Contains Duplicate II 包含重复值之二的相关文章

[LeetCode] Contains Duplicate III 包含重复值之三

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 参考资料: https://leetcode.com/discuss/381

【LeetCode-面试算法经典-Java实现】【217-Contains Duplicate(包含重复元素)】

[217-Contains Duplicate(包含重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice

LeetCode 219. Contains Duplicate II (包含重复项之二)

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 题目标签:Array, Hash Table 题目给了我们一个nums array 和一个 k, 让

[LeetCode] Contains Duplicate(II,III)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题思路 用一个set保存数组中的值,如

[LeetCode] Contains Duplicate 包含重复值

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 这道题不算难题,就是使用一个哈希表,遍历整个数组,如果哈希表里存在,返回fal

LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)

翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容. 如果任何值出现了至少两次,那么返回真(true), 如果每个值都是互不相同的,那么返回假(false). 原文 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it s

leetcode—217 Contains Duplicate(包含重复的数)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in t

LeetCode 217. Contains Duplicate (包含重复项)

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 题目标签:Array, Hash Table 题目给了我们一个nums arr

[LeetCode] Delete Duplicate Emails 删除重复邮箱

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------------+ | Id | Email | +----+------------------+ | 1 | [email protected] | | 2 | [email protected]