题目:

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 j is at most k.

翻译:

给定一个整数数组和一个整数k。找出是否存在下标i,j使得nums[i] = nums[j]。同一时候i,j的差值小于等于k。

分析:

遍历数组。使用map存储每一个值近期一次出现的下标。

代码:

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
Integer value=map.get(nums[i]);
if(value!=null&&i-value<=k){
return true;
}else{
map.put(nums[i],i);
}
}
return false;
}
}

Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]的更多相关文章

  1. Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...

  2. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  3. 219. Contains Duplicate II - LeetCode

    Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...

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

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

  5. Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  6. Leet Code OJ 338. Counting Bits [Difficulty: Medium]

    题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...

  7. Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

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

  9. [刷题] 219 Contains Duplicate II

    要求 给出整型数组nums和整数k,是否存在索引i和j,nums[i]==nums[j],且i和j之间的差不超过k 思路 暴力解法(n2) 建立最长为k+1的滑动窗口,用set查找窗口中是否有重复元素 ...

随机推荐

  1. 【BZOJ 3456】 3456: 城市规划 (NTT+多项式求逆)

    3456: 城市规划 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 658  Solved: 364 Description 刚刚解决完电力网络的问题 ...

  2. 【BZOJ 2179】 2179: FFT快速傅立叶 (FFT)

    2179: FFT快速傅立叶 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3308  Solved: 1720 Description 给出两个n位 ...

  3. js使用s:property标签接收json格式数据

    js使用s:property接收json数据时,会出现字符被转译的错误. 错误如下: 引号会被转译成'"'字符,导致解析不了. 错误原因: html的s:property接收不会出错,而js ...

  4. [BZOJ4028][HEOI2015]公约数数列(分块)

    先发掘性质: 1.xor和gcd均满足交换律与结合率. 2.前缀gcd最多只有O(log)个. 但并没有什么数据结构能同时利用这两个性质,结合Q=10000,考虑分块. 对每块记录这几个信息: 1.块 ...

  5. JZYZOJ1237 教授的测试 dfs

    http://172.20.6.3/Problem_Show.asp?id=1237   锻炼搜索的代码能力,不错的题. 开始对dfs到底向下传递什么搞不清楚,需要想一下,noip难度的题还有这种情况 ...

  6. Android UI设计规范之常用单位

    px :全称--pixel .像素.例如,480*800的屏幕在横向有320个象素,在纵向有480个象素. 屏幕的分辨率 : 屏幕的长宽方向上像素点的数量. dp(dip) : 全称--Density ...

  7. Unity JsonFx 插件使用

    在Unity中使用 JsonFx 插件笔记(提示:以下在 Unity3D v5.4.0 版本 Win 平台下测试成功) 下载 JsonFx 插件注意:JsonFx 插件其实就是一个 dll 文件(如果 ...

  8. [转][Android]Android数据的四种存储方式

    android.database.sqlite类 SQLiteQueryBuilder java.lang.Object android.database.sqlite.SQLiteQueryBuil ...

  9. Codeforces Beta Round #10 D. LCIS 动态规划

    D. LCIS 题目连接: http://www.codeforces.com/contest/10/problem/D Description This problem differs from o ...

  10. 普通主板设置BIOS实现电脑插电自动启动

    说明: 1.为什么要实现这种功能,很多时候在民间都基本用普通PC来做小型服务器,公司的私服等等,而这些普通PC在民用电环境中经常会停电,一停就会导致服务器不能自动来电重启,所以这个功能来点开机是必须的 ...