Contains Duplicate II
2015-12-22 00:32
525 查看
package cn.edu.xidian.sselab.array; import java.util.HashMap; import java.util.HashSet; import java.util.Set; /** * * @author zhiyong wang * title: Contains Duplicate II * content: * 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,可以比k小,所以不能用等于,要用小于等于 * */ public class ContainsDuplicateII { //多个if判断条件,如果可以写到一起,就用&&或者||操作写到一起,尽量不要用多级判断,一方面是形式上不好看,另一方面增加了时间的开销 //这里是用HashMap记录数值出现的下标与数值,当有重复数值出现,且下标值小于等于指定的最大值k时,返回正确的结果 public boolean containsNearbyDuplicate(int[] nums,int k){ int length = nums.length; HashMap<Integer,Integer> containers = new HashMap<Integer,Integer>(); for(int i=0;i<length;i++){ if(containers.containsKey(nums[i]) && i-containers.get(nums[i])<=k){ return true; }else{ containers.put(nums[i], i); } } return false; } //通过Set来进行判断 public boolean containsNearbyDuplicates(int[] nums,int k){ int start = 0; int end = 0; int length = nums.length; Set<Integer> containers = new HashSet<Integer>(); for(int i=0;i<length;i++){ if(!containers.contains(nums[i])){ end++; containers.add(nums[i]); }else return true; if(end - start > k){ containers.remove(nums[start]); start++; } } return false; } } |
相关文章推荐
- Daily Scrum NO.10
- C++迭代器失效http://blog.csdn.net/vipvipvip/article/details/6295973
- Daily Scrumming* 2015.12.21(Day 13)
- Yocto tips (14): Yocto中编译链的配置变更(tuning)
- 认识retain copy assign strong weak 的区别
- LeetCode 11 - Container With Most Water
- 使用Commons Email发送邮件
- 让机器人模块 PyAIML 平滑处理中文
- ACboy needs your help again!(杭电1702)(栈和队列的基础应用)
- 多线程wait-notify;notifyall.sleep
- SHU1952 Odd Pairs(维护前缀和)
- 派生类到基类的转换 和基类到派生类的转换 http://blog.csdn.net/wyyzsl212328/article/details/8263959
- 博弈算法实现简单五子棋
- MHA自动Failover过程解析(updated)(转)
- postfix mail command not found
- “Operation could not be completed. (SKErrorDomain error 0.)”
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- 解决svn Authorization failed错误【SVN权限问题】
- Daily Scrum - 12/15-21
- _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义