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 ...
随机推荐
- Cocos2d-x中Vector使用
1.创建Vector对象 Vector().默认的构造函数. Vector(ssize_t capacity).创建Vector对象,并设置容量. Vector(const Vector<T&g ...
- urlconnection.connect()和url.openconnection()的区别
urlconnect()ion.connect()()方法是抽象的:打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接). 如果在已打开连接(此时 connect()ed 字段的值为 tr ...
- 15.禁用ViewState
默认情况下ASP.net是启用ViewState的,这样在页面中会生成冗长的隐藏字段,ViewState对于需要PostBack处理的页面才可能有用,对于不需要交互的页面则完全没有必要用ViewSta ...
- cognos启动报错
[ ERROR ] Content Manager is unable to process your request because an unexpected event occurred in ...
- 【HDOJ】1818 It's not a Bug, It's a Feature!
状态压缩+优先级bfs. /* 1818 */ #include <iostream> #include <queue> #include <cstdio> #in ...
- Unity笔记
1.使某个对象上的脚本失效和生效: GameObject.Find("xxx").transform.GetComponent<xxx>().enabled = fal ...
- C# 精准获取代码运行时间
纯粹转载,转载请注明参考链接及作者! 参考链接:http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html,作者:博客园 大佬辉 ...
- Cut the Cake(大数相乘)
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly o ...
- Linux Kernel ‘test_root()’函数本地拒绝服务漏洞
漏洞名称: Linux Kernel ‘test_root()’函数本地拒绝服务漏洞 CNNVD编号: CNNVD-201306-432 发布时间: 2013-06-25 更新时间: 2013-06- ...
- 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。
原文网址:http://blog.csdn.net/hxdanya/article/details/39371759 由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. ...