LeetCode 219. Contains Duplicate II

原创 2018年04月16日 14:18:14

问题描述

  • 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.
  • 地址

问题分析

  • 该题是 LeetCode 217. Contains Duplicate 的进阶题目。
  • 该题是 滑动窗口 + HashSet 的典型例子。 HashSet用于实时统计窗口内每个数字是否出现过,滑动窗口用于控制窗口大小,使窗口内不含重复数字。

经验教训

  • 滑动窗口 + HashMap(HashSet) 的 使用

代码实现

class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k < 0 ) {
            return false;
        }
        HashSet<Integer> set = new HashSet<>();
        //初始化窗口
        int left = 0;
        int right = 0;
        while (right < nums.length) {
            if (right - left > k) {//若nums[right]的入窗导致了窗口 > k
                //左侧出窗,并更新set
                set.remove(nums[left]);
                ++left;
            }
            //若当前窗口已有 nums[right]
            if (set.contains(nums[right])) {
                    return true;
            }
            //入窗,并继续扩窗(不管当前左侧出不出窗,下一步都是扩窗,因为窗口尺寸永远不会缩小)
            set.add(nums[right]);
            ++right;
        }
        return false;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79960038

【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】

【219-Contains Duplicate II(包含重复元素II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-29 06:36:21
  • 3614

leetcode 219: Contains Duplicate II

leetcode 219: Contains Duplicate II java c++ python
  • xudli
  • xudli
  • 2015-05-29 15:02:09
  • 8591

219. Contains Duplicate II [easy] (Python)

题目链接https://leetcode.com/problems/contains-duplicate-ii/题目原文 Given an array of integers and an int...
  • coder_orz
  • coder_orz
  • 2016-06-14 19:59:14
  • 1953

LeetCode 219:Contains Duplicate II

Given an array of integers and an integer k, find out whether there there are two distinct indices i...
  • sunao2002002
  • sunao2002002
  • 2015-05-31 18:41:22
  • 3821

leetcode_c++:Contains Duplicate II(219)

题目Given an array of integers and an integer k, find out whether there are two distinct indices i and...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-06-18 13:33:40
  • 310

[C语言][LeetCode][219]Contains Duplicate II

题目Contains Duplicate II Given an array of integers and an integer k, find out whether there are tw...
  • Timsley
  • Timsley
  • 2015-08-28 22:45:07
  • 901

LeetCode219 ContainsDuplicateII java题解

题目: Given an array of integers and an integer k, find out whether there there are two distinct in...
  • u012249528
  • u012249528
  • 2015-07-02 17:50:06
  • 457

leetcode-java-219. Contains Duplicate II

/* Given an array of integers and an integer k, find out whether there are two distinct indices i an...
  • github_34514750
  • github_34514750
  • 2016-08-27 14:53:50
  • 398

leetcode笔记:Contains Duplicate II

题目的大意是,给定一个整数数组与一个整数k,当且仅当存在两个不同的下标i和j,满足:nums[i] = nums[j],且| i - j | ...
  • liyuefeilong
  • liyuefeilong
  • 2016-02-24 18:31:03
  • 1593

Leetcode—Contains Duplicate II—Python

class Solution: # @param {integer[]} nums # @param {integer} k # @return {boolean} d...
  • u011581550
  • u011581550
  • 2015-06-17 20:07:53
  • 869
收藏助手
不良信息举报
您举报文章:LeetCode 219. Contains Duplicate II
举报原因:
原因补充:

(最多只允许输入30个字)