(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 containsNearbyDuplicate(int[] nums, int k) {
int len=nums.length;
for(int i=0;i<len;i++){
for(int tmp=1;tmp<=k && i+tmp<len;tmp++)
if(nums[i]==nums[i+tmp])
return true;
}
return false;
}
}
运行超时:

方法2:分析:
代码如下:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
int start = 0, end = 0;
for(int i = 0; i < nums.length; i++){
if(!set.contains(nums[i])){
set.add(nums[i]);
end++;
} else
return true;
if(end - start > k) {
set.remove(nums[start]);
start++;
}
}
return false;
}
}
运行结果:
(easy)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 ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- [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 ...
随机推荐
- Android 三种方式实现自定义圆形页面加载中效果的进度条
转载:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76872 一.通过动画实现 定义res/anim/loading.xml如 ...
- ExtJs学习之Window
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 决策树模型组合之(在线)随机森林与GBDT
前言: 决策树这种算法有着很多良好的特性,比如说训练时间复杂度较低,预测的过程比较快速,模型容易展示(容易将得到的决策树做成图片展示出来)等.但是同时, 单决策树又有一些不好的地方,比如说容易over ...
- eclipse项目持续报红解决
1.tomcat 同步,点击publish: 2.clear项目 3.项目报红,Maven --->UpdateProject 4.pom.xml需要更新,下载最新jar包,附图:
- jquery动画效果---animate()--滚屏
jquery动画效果---animate()方法---W3school
- ckeditor中“浏览服务器”的后台操作
此博文,基于CKeditor 4.5.6版本测试通过. 原创博文,转载请注明出处 参考官方文档,以及网络上的一些帖子.经过调试得到正确的期待中的结果. [网络上的一些所谓的帖子,不知道是故意将上传的代 ...
- 【FreeMaker】FreeMaker学习-基础
转载请标明出处:http://www.cnblogs.com/ssslinppp 阅读目录 -04-08 08:08:08 Pacific Daylight Time Tue, Apr 8, '03 ...
- S3C2440之MMU
转自:http://blog.chinaunix.net/uid-23193900-id-3187782.html 1.MMU简介 MMU(Memory Management Unit),内存管 ...
- 黄聪:C#如何操作JSON数据(读取、分析)
使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/).下载后加入工程就能用.通常可以使用JObject, JsonReader, JsonWrit ...
- spring学习笔记2(转)
1.在Java开发领域,spring相对于EJB来说是一种轻量级的,非侵入性的Java开发框架,曾经有两本很畅销的书<Expert one-on-one J2EE Design and Deve ...