LeetCode 219. Contains Duplicate II ***** 滑动窗口,查找表 217.Contains Duplicate

来源:互联网 发布:阿里云的好处 编辑:程序博客网 时间:2024/06/18 06:35

    • Contains Duplicate II 题目
      • 题意
      • 注意
      • 思路
      • 代码
      • 结果
    • Contains Duplicate 题目
      • 题意
      • 思路
      • 代码
      • 结果

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 absolute difference between i and j is at most k.

题意

给定一个整型数组nums和一个整型k,查找在数组中是否存在nums[i] == nums[j],并且j-i<=k.

注意

  • 数组元素个数小于2和k<=0直接返回
  • 为满足j-i<=k.在区间[0,k]内有k+1个元素

思路

根据题意在区间[l,r]内找满足条件的元素,这个区间应该是从左到右滑动的,那么应该想到了滑动窗口,只是这个窗口的长度是固定的 。可变窗口参考Minimum Size Subarray Sum.
这样思路就很清晰了,初始化[l,r]窗口长度为0,通过扩大右边界,使窗口长度增加,此过程中记录元素出现的次数,当新增加的元素在表中则返回true。当窗口扩大到k时,缩小左边界,对应的元素在表中减减。
这里写图片描述

代码

//时间复杂度:O(N)//空间复杂度:O(K)  //通过map实现class Solution {public:    bool containsNearbyDuplicate(vector<int>& nums, int k) {       //j-i<=k       //[l,r]中需要有K+1个元素  [0,K]有k+1个元素        if (nums.size() < 2||k==0) return false;        int l = 0;        unordered_map<int, int> record;        for (int r = 0; r < nums.size();r++)        {            //值判断非零情况            if (record[nums[r]] > 0)            {                return true;                //cout << "true" << endl;            }            else            {                record[nums[r]]++;                //保持窗口的长度为k,注意边界条件                if (l + 1 < nums.size()&&r-l>=k/*&&l<r*/)                {                    //当窗口内k+1个元素都不存在相等元素,需要调整左边界,为了让右边界滑动                    record[nums[l]]--;                    l++;                }            }        }        return false;    }};
// 时间复杂度: O(n)// 空间复杂度: O(k)  //通过set实现class Solution {public:    bool containsNearbyDuplicate(vector<int>& nums, int k) {        if( nums.size() <= 1 )            return false;        if( k <= 0 )            return false;        unordered_set<int> record;        for( int i = 0 ; i < nums.size() ; i ++ ){            if( record.find( nums[i] ) != record.end() )                return true;            //set插入元素的方式和map还是有区别的            record.insert( nums[i] );            // 保持record中最多有k个元素            // 因为在下一次循环中会添加一个新元素,使得总共考虑k+1个元素            //[i,i+k]            if( record.size() == k + 1 )                record.erase( nums[i-k] );        }        return false;    }};

结果

这里写图片描述

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.

题意

给定一个整型数组,查找该数组是否有重复元素,如果有返回true。否则返回false.

思路

很简单的一道题,hash查找表,边添加边查找

代码

class Solution {public:    bool containsDuplicate(vector<int>& nums) {        unordered_map<int,int> record;        for(int i=0;i<nums.size();i++)        {            record[nums[i]]++;            if(record[nums[i]]>1)            {                return true;            }        }        return false;    }};

结果

这里写图片描述

原创粉丝点击