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.

判断数组是是否含有重复元素。

思路比较多,暴力查找每个元素、排序后查找、map统计每个元素出现的次数、set不包含重复元素。

有个小技巧,set可以用于去除数组中的重复元素,并且还有排序功能。map可以统计每个元素出现的次数,也有排序功能

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return set<int>(nums.begin(),nums.end()).size() < nums.size();
}
};

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.

与上一题类似,不过本题每次查找重复元素的数据范围变为k,也就是希望我们自行维护一个大小为k的滑动窗口。

关于滑动窗口的表示,可以把窗口内的数据装入一个容器内(set,map,vector等),也可以用下标维护一个范围表示,不同的题目灵活选择

关于set/unordered_set中insert的返回值,其返回pair<set/uset迭代器,bool>,第二布尔值为true表示成功插入,为false表示已有该元素,具体可查看cpp reference

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_set<int> windows; for(int i=;i<nums.size();++i){
if(i>k){
windows.erase(nums[start-]);
} if(windows.insert(nums[i]).second == false){
return true;
}
} return false;
}
};

220. Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k.

这题的解题框架还是滑动窗口,不过,并不是判断窗口内是否有重复元素,而是判断窗口内两数的差值关系。如果直接用暴力求得窗口内两数的差值是否小于等于t,需要o(k*k)的时间复杂度,一共有n-k个窗口,如果按照这个思路写代码,会超时。所以另寻出路。暴力其实是没目的的枚举所有数对,而现在我们知道,需要窗口内的两数满足 |a-x|<=t的关系,也就是 a-t =< x <= a+t,所以我们可以用查找的思路,每次往窗口丢入一个数据的时候,检查是否有[a-t,a+t]这个范围内的数据在窗口中。

假设使用set存储窗口中的数据,那么使用lower_bound函数就可以在o(lgk)时间内找到第一个大于等于a-t的这个数据,再检查一下找到的数据是否小于等于a+t即可

class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { set<int> windows; for(int i=;i<nums.size();++i){ if(i>k){
windows.erase(nums[i-k-]);
} auto iter = windows.lower_bound(nums[i]-t); if(iter != windows.end() && *iter <= nums[i] + t){
return true;
} windows.insert(nums[i]);
}
return false;
}
};

这三个题的关键词是,滑动窗口,窗口内的数据间的关系

Contains Duplicate,Contains Duplicate II,Contains Duplicate III的更多相关文章

  1. Remove Duplicate Letters I & II

    Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...

  2. [Swift]LeetCode219. 存在重复元素 II | Contains Duplicate II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  3. LeetCode 219: 存在重复元素 II Contains Duplicate II

    题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. ​ Given an ...

  4. 查找表 219.Contains Duplicate(2),217 Contain Duplicate, 220(3)

    思路:滑动窗口(长度为k+1)看这个窗口里的是否有两个元素的值相同.加查找表. //时间:O(n) //空间:O(k) class Solution { public: bool containsNe ...

  5. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  6. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  7. Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III

    题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  8. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  9. RMAN_学习实验2_RMAN Duplicate复制数据库过程(案例)

    待整理 对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rm ...

随机推荐

  1. eclipse python开发环境搭建

    eclipse python开发环境搭建[非原创] 1.在www.eclipse.org官网下载Eclipse Classic 4.2.2,Win7 64位下载eclipse-SDK-4.2.2-wi ...

  2. mysql索引之唯一索引

    mysql 的唯一索引一般用于不重复的字段,一般会把表中的id设为唯一索引,创建唯一索引的目的不是为了提高查询速度,而是为了避免数据重复,注意:唯一索引可以有多个,但是列值必须唯一,创建唯一索引使用关 ...

  3. C++ Primer 5th 第1章 开始

    *****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...

  4. ThinkPHP内置函数详解D、F、S、C、L、A、I

    单字母函数D.F.S.C.L.A.I 他们都在ThinkPHP核心的ThinkPHP/Mode/Api/functions.php这个文件中定义. 下面我分别说明一下他们的功能: D() 加载Mode ...

  5. While reading XXX pngcrush caught libpng error: N

    错误一:   While reading /XXX/XXX/XXX/img1.png pngcrush caught libpng error:   Not a PNG filCould not fi ...

  6. ubuntu 12.04 下搭接Qt 嵌入式开发环境

    1.安装前的准备工作 (1)有ubuntu12.04 的系统镜像(也可以其他linux 如Fedorea9),都是安装好的 (2)虚拟机VMWare 或 VirtualBox ,两者都可以,都是安装好 ...

  7. hdu 1385 Minimum Transport Cost

    http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...

  8. 【Xamarin 在Mac OS 上的部署安装环境】

    ******************没用Mac 的机子,也只能靠虚拟机了**********1 安装VMware 10 从网上下载即可2 下载MAC OS 10.9.5的安装镜像,网上有很多,最好使用 ...

  9. Android Content Provider简介

    Content Provider是Android的四大组件之一,与Activity和Service相同,使用之前需要注册: Android系统中存在大量的应用,当不同的应用程序之间需要共享数据时,可以 ...

  10. bzoj3629[JLOI2014]聪明的燕姿

    http://www.lydsy.com/JudgeOnline/problem.php?id=3629 搜索. 我们知道: 如果$N=\prod\limits_{i=1}^{m}p_{i}^{k_{ ...