首页 > 其他 > 详细

leetcode 219. Contains Duplicate II

时间:2019-04-07 23:23:22      阅读:118      评论:0      收藏:0      [点我收藏+]

Keep a window of width k, before visit new element, remove nums[i - k - 1].
Other details are much the same as Contains Duplicate.

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

leetcode 219. Contains Duplicate II

原文:https://www.cnblogs.com/exhausttolive/p/10667923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!