[LeetCode]--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 j is at most k.

想法:这题比较简单,可以直接定义一个长度最大为k的滑动窗口,用一个set维护窗口内的数字判断是否出现重复,使用两个指针start和end标记滑动窗口的两端,初始都是0,然后end不断进行扩展,扫描元素判断是否出现重复元素,直到发现end-start>k, 就开始移动start,并且在set中移除对应的元素。如果以为扫描到数组末尾还没有发现重复元素,那就可以返回false。时间复杂度和空间复杂度都是O(N)。

Set<Integer> appearedNum = new HashSet<Integer>();
        int start = 0, end = 0;
        for (int i = 0; i < nums.length; i++) {
            if (!appearedNum.contains(nums[i])) {
                appearedNum.add(nums[i]);
                end++;
            } else
                return true;
            if (end - start > k) {
                appearedNum.remove(nums[start]);
                start++;
            }
        }
        return false;
时间: 2024-09-21 09:26:45

[LeetCode]--219. Contains Duplicate II的相关文章

[LeetCode] Contains Duplicate(II,III)

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. 解题思路 用一个set保存数组中的值,如

[LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output:

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode]113.Path Sum II

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [分析] 题目要求求出所有路径

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

[LeetCode]59.Spiral Matrix II

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [分析] 模拟 [代码] /**-------------------------

[LeetCode]*137.Single Number II

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数数组,每个元素出现了三次,除了一个.找出那

leetcode 217 Contains Duplicate 数组中是否有重复的数字

 Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

[LeetCode] Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 解题思路 设链表长度为n,头结点与循环节点之间的长度为k.定义两个指针slow和fast,slow每次走一步,fast每次走两步.当两个指针相遇时,有: fast = slow * 2 fast -