219. Contains Duplicate II

题目:

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 jis at most k.

链接: http://leetcode.com/problems/contains-duplicate-ii/

题解:

用HashMap来保存(nums[i],i) pair,假如存在相同key并且 i - map.get(key) <= k,返回true。否则遍历完毕以后返回false.

Time Complexity - O(n), Space Complexity - O(n)

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

 

二刷:

跟一刷一样,使用一个Map来保存数字以及数字的index,然后进行比较。假如相同并且 i - map.get(nums[i]) <= k,那么我们返回true。否则遍历完毕以后返回false.

Java:

Time Complexity - O(n), Space Complexity - O(n)

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

 

三刷:

方法还是和一刷,二刷一样,用一个HashMap来保存元素和位置。这里最好给HashMap设定一个初始的大小来避免resizing带来的cost。最大的test case好像是30000,我们设置30000 / 0.75 = 40000左右就可以了。

Java:

Time Complexity - O(n), Space Complexity - O(n)

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

 

使用Set以及sliding window。

  1. 我们维护一个size为k的HashSet
  2. 遍历整个数组,每次先判断是否有重复,有重复的话我们直接返回true
  3. 当size > k的时候,这时候size() = k + 1。 我们要把距离当前元素最远的一个元素,即第i - k个元素移出set,继续维护set的size = k
  4. 全部遍历完毕以后返回false。
public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums == null || k <= 0) {
            return false;
        }
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!set.add(nums[i])) {
                return true;
            }
            if (set.size() > k) {
                set.remove(nums[i - k]);
            }
        }
        return false;
    }
}

 

Update:

利用HashMap的put。创建map的时候利用load factor = 0.75,我们建立一个比nums.length略大一些的map就能节约resizing的时间,但这也是空间换时间了。

 

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

 

Update:重写了使用set的Sliding Window方法

public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums == null || k <= 0) return false;
        Set<Integer> set = new HashSet<>((int)(nums.length / 0.8));
        for (int i = 0; i < nums.length; i++) {
            if (!set.add(nums[i])) return true;
            if (set.size() > k) set.remove(nums[i - k]);
        }
        return false;
    }
}

 

Update:  SouthPenguin的Sliding window方法。 最优解。

Worst case: Time Complexity - O(n), Space Complexity - O(n)

public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums == null || k <= 0) return false;
        Set<Integer> set = new HashSet<>((int)(nums.length / 0.8));
        for (int i = 0; i < nums.length; i++) {
            if (i > k) set.remove(nums[i - k - 1]);
            if (!set.add(nums[i])) return true;
        }
        return false;
    }
}

 

 

 

 

Reference:

https://leetcode.com/discuss/38445/simple-java-solution 

转载于:https://www.cnblogs.com/yrbbest/p/4990419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值