Leetcode Contains Duplicate II
2015-09-22 01:14
591 查看
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.
Java code:
解题思路:
想到了一种结构map, key 是该值,value是该值对应的前一个index.Java code:
public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer, Integer> values = new HashMap<Integer, Integer>(); //first is value, second is current index for(int i = 0; i< nums.length; i++){ if(values.containsKey(nums[i])){ int x = values.get(nums[i]); if((i-x) <= k) { return true; } } values.put(nums[i], i); } return false; }
相关文章推荐
- LightOJ 1236 Pairs Forming LCM(算术基本定理)
- Leetcode Contains Duplicate
- main()参数int argc,int *argv[]的学习
- Light oj 1086 - Jogging Trails(状压dp 将图变成欧拉图)
- Sublime Text 2 - There are no packages available for installation 解决方法(亲测可行)
- E: 无法获取锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)解决方法
- exit()和_exit()函数 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html
- Codeforces__Raising Bacteria
- Codeforces__Raising Bacteria
- xCode 7 编译出现错误:does not contain bitcode 解决办法
- 【转】友好的 ContainerView & AutoLayout 资料推荐
- [置顶] Linux DM9000网卡驱动程序完全分析 http://blog.csdn.net/ypoflyer/article/details/6209922
- AI-Tank
- 查询表锁争用情况 table_locks_waited & table_locks_immedaite
- setContentView(R.Layout.main)的意思
- raid5实现原理
- RAID5服务器磁盘阵列,两块盘坏了数据恢复
- http://blog.csdn.net/abcjennifer/article/details/7716281 过拟合问题
- 为什么使用HttpUrlConnection时候,用available方法不准确
- failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hiber