资讯专栏INFORMATION COLUMN

leetcode217.219.220 contains duplicate

tulayang / 1668人阅读

摘要:输入一个整数数组,查看数组中是否存在重复的值。新的数组中数组的下标为原数组的值,如果遍历过,则设置为。这里使用了作为实现的数据结构,通过堆的形式对集合中的数据进行存储,从而我们可以通过某种顺序获得该集合中的所有顺序。

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 map = new HashMap();
        
        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 indexMap = new HashMap();
        for(int i = 0 ; i
思路二:set

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

    public boolean containsNearbyDuplicate2(int[] nums, int k){
        Set potentialSet = new HashSet();
        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 potentialNums = new TreeSet();
            for(int i = 0 ; i= curNum) return true;
                potentialNums.add(curNum);
                if(i >= k) potentialNums.remove((long)nums[i-k]);
            }
            return false;
    }


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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70759.html

相关文章

  • [Leetcode] Contains Duplicate 包含重复

    摘要:代码集合法复杂度时间空间思路同样使用集合,但这次我们要维护集合的大小不超过,相当于是记录一个宽度为的窗口中出现过的数字。 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. Your function should return true if any v...

    rozbo 评论0 收藏0
  • [LeetCode] Contains Duplicate

    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 ...

    褰辩话 评论0 收藏0
  • Leetcode PHP题解--D90 217. Contains Duplicate

    摘要:题目链接题目分析返回给定的数组中是否有元素重复出现。思路用和即可最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D90 217. Contains Duplicate 题目链接 217. Contains Duplicate 题目分析 返回给定的数组中是否有元素重复出现。 思路 用count和array_unique即可 最终代码

    mingde 评论0 收藏0
  • leetcode 217 Contains Duplicate

    摘要:题目详情输入一个整数的数组,如果数组中的元素有重复的,那么返回,如果数组中的元素都是唯一的,那么返回思路这道题理解起来比较简单,首先还是要注意一下边界条件异常输入,对于长度小于等于的数组做一个直接的返回对于这种要考虑数组中元素的重复的问题, 题目详情 Given an array of integers, find if the array contains any duplicate...

    philadelphia 评论0 收藏0
  • [LintCode/LeetCode] Contains Duplicate II

    Problem 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 ...

    894974231 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<