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的更多相关文章

  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. Java [Leetcode 219]Contains Duplicate II

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

  8. C#解leetcode 219. Contains Duplicate II

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

  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. zookeeper启动入口

    最近正在研究zookeeper,一些心得记录一下,如有错误,还请大神指正. zookeeper下载地址:http://zookeeper.apache.org/releases.html,百度一下就能 ...

  2. 使用async属性异步加载执行JavaScript

    HTML5让我兴奋的一个最大的原因是,它里面实现的新功能和新特征都是我们长久以来一直期待的.比如,我以前一直在使用placeholders,但以前必须要用JavaScript实现.而HTML5里给Ja ...

  3. (转帖)BootStrap入门教程 (二)

    上讲回顾:Bootstrap的手脚架(Scaffolding)提供了固定(fixed)和流式(fluid)两种布局,它同时建立了一个宽达940px和12列的格网系统. 基于手脚架(Scaffoldin ...

  4. mybatis insert前获取要插入的值

    <insert id="insertRequestItem" parameterType="requestItemModel"> <selec ...

  5. [转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)

    首先说几个最常用的关键字,“eq” 和 “==”等同,可以使用 “and” 表示并且,“or”表示或者.“!" 和 "not” 都表示取反. 一.针对wireshark最常用的自然 ...

  6. SPOJ #442 Searching the Graph

    Just CS rookie practice on DFS\BFS. But details should be taken care of: 1. Ruby implementation got ...

  7. android学习笔记45——android的数据存储和IO

    android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...

  8. golang的的模板引擎之pongo2

    https://github.com/flosch/pongo2 beego的扩展包 https://github.com/yansuan/beego-pongo2 gin的扩展包 https://g ...

  9. 49. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  10. 【转】SVN服务器客户端以及环境的搭建和使用

    vss,cvs,svn三者都是版本控制工具 vss是锁定-编辑-解锁模式,svn虽然也支持锁定,但默认是修改-冲突-合并模式 vss的版本号对应的是单个文件,svn的版本号对应的是整个版本库 vss是 ...