题目:
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 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  2. Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  3. Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  4. Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. Leet Code OJ 326. Power of Three [Difficulty: Easy]

    题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  6. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

    题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  7. Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]

    题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursiv ...

  8. Leet Code OJ 38. Count and Say [Difficulty: Easy]

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...

  9. 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 思路分析: 题意是将二叉树所有左右子数 ...

最新文章

  1. 数值计算领域的“圣经”,图灵出了新版本 | 11月书讯
  2. “面试不败计划”:各大公司Java后端开发面试题总结
  3. Python教程:跳出多层循环for、while
  4. python CMAKE的使用
  5. spark 应用程序性能优化:12 个优化方法
  6. app把信息添加到mysql_如何将数据从iphone app上传到mysql数据库
  7. warning C4996: 'fopen': This function or variable may be unsafe.(_CRT_SECURE_NO_WARNINGS)
  8. Python实现图像直方图均衡化算法
  9. 防火墙配置文件iptables详解
  10. VC++ 只运行一个程序实例
  11. 项目管理10大模板Excel版(可直接套用)
  12. C++ 获取鼠标点击位置
  13. apipost自动化测试工具
  14. 关于DBC文件的格式解析(转)
  15. c语言交通处罚单系统主函数,C语言课程设计交通处罚单管理系统.doc
  16. C# 中取绝对值的函数
  17. TQ2440内核linux2.6.28移植
  18. github官网进不去解决方案
  19. Java实现mds降维_降维算法MDS
  20. 安装Office365后版本信息显示为Office2016

热门文章

  1. Three.js之渲染器
  2. 指数型生成函数[bzoj3456]城市规划
  3. cocos2d-x初探学习笔记(21)--精灵类
  4. ADO学习(六)服务器和客户端游标
  5. CTO要我把这份MySQL规范贴在工位上!
  6. Java 开发人员 2019 生态系统信息图
  7. linux下使用C++操作mysql
  8. 【LiveVideoStack线上分享】— 视频生产环境下的音视频解决方案
  9. 如何构建分布式SFU/MCU媒体服务器?
  10. Netflix:通过可视化和统计学改进用户QoE