217 Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

输入一个整数数组,查看数组中是否存在重复的值。

思路一:hashmap or hashset

使用java中的数据结构将已经遍历起来的值存储起来,然后查询当前的值是否已经便利过

    public boolean containsDuplicate(int[] nums) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        
        for(int curNum : nums){
            if(map.containsKey(curNum)) return true;
            else map.put(curNum, 1);
        }
        return false;
    }

思路二:min-max重映射

获得该整数数组的最大值和最小值,并且利用最大值和最小值将原数组映射到新的数组。新的数组中数组的下标为原数组的值,如果遍历过,则设置为1。

    public boolean containsDuplicate2(int[] nums){
        if(nums==null || nums.length<=1) return false;
        int length = nums.length;
        int min = nums[0];
        int max = nums[0];
        for(int curNum : nums){
            if(curNum < min) min = curNum;
            if(curNum > max) max = curNum;
        }
        //如果数组中最大值和最小值之间的数字个数小于数组长度,则一定存在重复值
        if(max - min + 1< length) return true;
        
        int[] result = new int[max-min+1];
        for(int curNum : nums){
            int newIndex = curNum - min;
            if(result[newIndex] != 0) return true;
            else result[newIndex]++;
        }
        return false;
    }

219 Contains DuplicateII

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 absolute difference between i and j is at most k.

同I,还是整数数组,不同的是,要求两个重复值的之间的间隔不得超过k

思路一:hashmap

还是通过hashmap存储已经遍历过的选项以及最近一次遍历到时的下标值。如果重复数值的下标之间的值不超过k,那么就证明重复值满足条件

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
        for(int i = 0 ; i<nums.length ; i++){
            int curNum = nums[i];
            if(indexMap.containsKey(curNum)){
                int prevIndex = indexMap.get(curNum);
                if(i - prevIndex <= k){
                    return true;
                }else{
                    indexMap.put(curNum, i);
                }
            }else{
                indexMap.put(curNum, i);
            }
        }
        return false;
    }

思路二:set

保留一个含有最近k个值的集合,如果这个集合中存在和当前值相同的值,那么就存在相同的值,无需再去去判断相同值之间的下标是否符合要求。

    public boolean containsNearbyDuplicate2(int[] nums, int k){
        Set<Integer> potentialSet = new HashSet<Integer>();
        for(int index = 0 ; index < nums.length ; index++){
            int curNum = nums[index];
            if(potentialSet.contains(curNum)){
                return true;
            }
            potentialSet.add(curNum);
            
            if(potentialSet.size() > k) potentialSet.remove(nums[index-k]);
        }
        return false;
    }

220 Contains DuplicateIII

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

这题在II的基础上重新定义了相同的条件,也就是如果两个值之间的绝对差不超过t,那么就可以称这两个值相同。
这里使用了treeset作为实现的数据结构,treeset通过堆的形式对集合中的数据进行存储,从而我们可以通过某种顺序获得该集合中的所有顺序。

    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
            if(nums == null || nums.length <= 1 || k == 0) return false;
            
            TreeSet<Long> potentialNums = new TreeSet<Long>();
            for(int i = 0 ; i<nums.length ; i++){
                long curNum = nums[i];
                
                long ceilVal = curNum - t;
                Long ceiling = potentialNums.ceiling(ceilVal);
                
                long floorVal = curNum + t;
                Long floor = potentialNums.floor(floorVal);
                
                if(ceiling != null && ceiling <= curNum)return true;
                if(floor != null && floor >= curNum) return true;
                potentialNums.add(curNum);
                if(i >= k) potentialNums.remove((long)nums[i-k]);
            }
            return false;
    }

clipboard.png
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~


raledong
2.7k 声望2k 粉丝

心怀远方,负重前行