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 = ; 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的更多相关文章
- [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 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- 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 ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- 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 ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
- (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 ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- [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 ...
随机推荐
- openerp service install
cp openerp.init to /etc/init.d/openerp update-rc.d openerp default PATH=/sbin:/usr/sbin:/bin:/ ...
- 使用OpenSSL API进行安全编程
http://www.ibm.com/developerworks/cn/linux/l-openssl.html OpenSSL API 的文档有些含糊不清.因为还没有多少关于 OpenSSL 使用 ...
- Python3 time()
在<Python基础教程(第二版)>一书中, if time % 60 == 0 : print 'on the hour! '在3.3.2版本中显示错误.于是自己查了一下帮助文档,也在网 ...
- 搞Solr这一年(本人QQ 282335345 群412268049 欢迎大家一起学习Solr 非诚勿扰)
搞Solr这一年 去年6月份毕业到现在已经快一年半了,很庆幸从事了搜索引擎这份工作,虽然谈不上有多深入,但至少已经入门了.在这一年半里,搞了3个月的hbase和mapreduce,搞了一个月的nutc ...
- BZOJ 1036 树的统计
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- Can deep learning help you find the perfect girl?
Can deep learning help you find the perfect girl? One of the first things I did when I moved to Mont ...
- JavaScript的应用
DOM, BOM, XMLHttpRequest, Framework, Tool (Functionality) Performance (Caching, Combine, Minify, JSL ...
- C# IOCP服务器
C# IOCP服务器 @by 群63438968 这是我写的unity 网络斗地主的服务器源码,本来我想只公开部份服务端代码的,但是为了回报大家的热情,以及曾经和现在的好兄弟,我计划慢慢开源! 告诉 ...
- QT 线程暂停,继续执行的一种实现(有些道理,而且封装了)
注意:本次实现线程的暂停执行主要采用互斥量的方法,如果有更好的实现方法的小伙伴可以在下面留言! 直接插入代码了,由于做的小demo,代码写的可能有点乱,但还算完整. 1 2 3 4 5 6 7 8 9 ...
- AndroidStudio SVN检出
版本管理是每个项目的必经之路,很多的ADT都会集成版本管理插件.AS也同样可以集成GITHUB和SVN插件.github对项目有一定的限制,而SVN就比较开放了,所以我们在用AS开发的时候一般用SVN ...