C#解leetcode 219. Contains Duplicate II

该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet 用法

题目:

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.

解答:

public class Solution {
    public bool ContainsNearbyDuplicate(int[] nums, int k) {
      HashSet<int> hashSet = new HashSet<int>();
      for (int i = 0; i < nums.Length; i++) {
            if (i > k) {
                hashSet.Remove(nums[i - k - 1]);
            }
            if (!hashSet.Add(nums[i])) {
                return true;
            }
        }

       return false;
    }
}

之所以可以用这个答案,主要是因为集合的一个特性:

在集合中所有的元素都只能存在一次,如果向集合中添加已经存在的元素会失败

时间: 2024-10-03 22:53:46

C#解leetcode 219. Contains Duplicate II的相关文章

leetCode 219. Contains Duplicate II 数组

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 difference between i and j is at most k. 题目大意: 找到数组中两个相同元素,如果这两个元素的距离小于等于k

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

Java [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 difference between i and j is at most k. 解题思路: HashMap解决. 代码如下: public class Solution { public

Java for LeetCode 219 Contains Duplicate II

Given an array of integers and an integer k, find out whether there 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. 解题思路: HashMap,JAVA实现如下: public boolean containsNearby

(easy)LeetCode 219.Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k. 方法1:暴力解法 代码如下: public class Solution { public boolean

LeetCode#219 Contains Duplicate II

Problem Definition: Given an array of integers and an integer k, find out whether there 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. Solution 1:  O(n^2)  (报超时) 1 def

Leetcode 219 Contains Duplicate II STL

1 class Solution { 2 public: 3 bool containsNearbyDuplicate(vector<int>& nums, int k) { 4 map<int,int> mii; 5 for (int i = 0;i<nums.size() ;++i) 6 { 7 if (mii.find(nums[i]) != mii.end()) 8 { 9 if(i - mii[nums[i]] <= k) return true; 1

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文: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] = nu

[LeetCode][JavaScript]Contains Duplicate II

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. https://leetcode.com/proble