码迷,mamicode.com
首页 > 其他好文 > 详细

Jan 11 - Contains Duplicate II; Array; Traverse; HashMap; HashSet;

时间:2016-01-12 07:44:44      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

代码:

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

 

HashMap 和 HashSet 相关函数的信息:

https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

https://msdn.microsoft.com/en-us/library/bb353005(v=vs.110).aspx

Jan 11 - Contains Duplicate II; Array; Traverse; HashMap; HashSet;

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5123141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!