LeetCode OJ:Contains DuplicateII(是否包含重复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 difference between i and jis at most k.

这题上上一篇博客的延伸,问的是k长的距离内有没有两个数是相等的,类似一个华栋窗口问题,方法比较简单,使用一个map记下上次数出现的位置就可以了,代码如下:

 1 class Solution {
 2 public:
 3     bool containsNearbyDuplicate(vector<int>& nums, int k) {
 4         map<int, int> ret;
 5         int sz = nums.size();
 6         for (int i = 0; i < sz; ++i){
 7             if (ret.find(nums[i]) != ret.end() && i - ret[nums[i]] <= k)
 8                 return true;
 9             else
10                 ret[nums[i]] = i;
11         }
12         return false;
13     }
14 };
时间: 2024-10-24 05:09:09

LeetCode OJ:Contains DuplicateII(是否包含重复II)的相关文章

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 OJ 95. Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}&

LeetCode OJ Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

&amp;lt;LeetCode OJ&amp;gt; 78 / 90 Subsets (I / II)

Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2

LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2

[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 包含重复值的延伸,不同之处在于那道题只要我们

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, 让