该题用到了.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 = ; i < nums.Length; i++) {
if (i > k) {
hashSet.Remove(nums[i - k - ]);
}
if (!hashSet.Add(nums[i])) {
return true;
}
} return false;
}
}

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

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

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

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...

  3. 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 ...

  4. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  5. 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 ...

  6. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

  7. (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 ...

  8. Java [Leetcode 219]Contains Duplicate II

    题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...

  9. [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 ...

随机推荐

  1. 怎么屏蔽F5键刷新功能

    window.document.onkeydown=function(){if(event.keyCode==116){//屏蔽F5键,改为只刷新本页面,防止一刷就刷整个窗口event.keyCode ...

  2. Browsing History

    hdu4464:http://acm.hdu.edu.cn/showproblem.php?pid=4464 题意:就是统计n个字符串中每个字符串每个字符对印的Asci,然后输出最大的长度. 题解:水 ...

  3. Hibernate中的事务隔离

    在我们的项目中,老发现程序报告sesssion is closed或者因数据已经被其他事务修改而导致当前事务无法提交,由于系统的运行用户最多也就几十个人,所以考虑使用严格的事务隔离来防止这种类型的问题 ...

  4. C 函数原型

    int add(int,int);//add two int numbers and return it--- add function prototype; int main(int argc, c ...

  5. Linux Kernel 远程拒绝服务漏洞

    漏洞名称: Linux Kernel 远程拒绝服务漏洞 CNNVD编号: CNNVD-201307-309 发布时间: 2013-07-18 更新时间: 2013-07-18 危害等级:    漏洞类 ...

  6. 字符串(KMP):BZOJ 3670 [Noi2014]动物园

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1521  Solved: 813[Submit][Status] ...

  7. 汇编学习笔记(3)[bx]和loop

    本文是<汇编语言>一书的学习笔记,对应书中的4-6章. 汇编程序的执行 要想将源代码变为可执行的程序需经过编译.连接两个步骤,WIN7操作系统下需要MASM程序来进行编译连接工作.将MAS ...

  8. 解决android启动程序时,会出现一个短暂的白色空白界面的问题

    在启动的activity定义style为透明就可以了(android:theme="@android:style/Theme.Translucent")

  9. JS JQuery Ajax 跨域 Post Soap webservice

    呵呵 最近做一些HTML5的项目, 对于前段开发, 相信大家会碰到一个常见问题, 那就是Javascript跨域访问的问题. 话不多说 直接重点 当前网站和Webservice部署在同一个domain ...

  10. 软工UML学习札记

    UML模型由:事物.关系和图组成 (1)类(class)── 类用带有类名.属性和操作的矩形框来表示. (2)主动类(active class)── 主动类的实例应具有一个或多个进程或线程,能够启动控 ...