Contains Duplicate II

看了一天电影果然弱智啊

这么简单的题目居然bug

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

 

posted @ 2015-05-30 05:41  世界到处都是小星星  阅读(137)  评论(0编辑  收藏  举报