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. [转]Mac App distribution in App Store

    Mac程序的大包上传和iOS的有些许不同,因为Mac app既可以上传到store,也可以不通过store供人下载.因此,code sign和provision要根据情况(开发,release< ...

  2. uva 10107 - What is the Median?

    #include <cstdio> #include <iostream> using namespace std; ]; int main() { int i, cur_in ...

  3. SQL约束脚本的用法

    1.主键约束:要对一个列加主键约束的话,这列就必须要满足的条件就是分空因为主键约束:就是对一个列进行了约束,约束为(非空.不重复)以下是代码   要对一个列加主键,列名为id,表名为emp 格式为:a ...

  4. 浅谈JS中的闭包

    浅谈JS中的闭包 在介绍闭包之前,我先介绍点JS的基础知识,下面的基础知识会充分的帮助你理解闭包.那么接下来先看下变量的作用域. 变量的作用域 变量共有两种,一种为全局变量,一种为局部变量.那么全局变 ...

  5. 分享到QQ空间、新浪微博、腾讯微博的代码!(收藏)

    QQ空间分享代码如下:    <a href="javascript:void(0);" onclick="window.open('http://sns.qzon ...

  6. GO实例3 Slice append打印

    package main import "fmt" func main(){ ]int slice:=array[:] slice[]='a' slice[]='b' s1:=ap ...

  7. Mysql 新建用户以及授权远程连接操作

    1:以root身份登陆mysql终端 mysql -uroot -pmysql 2:创建wx用户,注意密码要加单引号 mysql> create user wx identified by 'w ...

  8. Visual Studio中的lib的链接顺序

    描述:如果有一个exe工程,它依赖于A.lib,B.lib,A.lib和B.DLL我同样有他们的源码工程.依赖顺序是这样的exe->A.lib->B.DLL.那么如果我改动了B的源码,编译 ...

  9. (转)失落的C语言结构体封装艺术

    目录1. 谁该阅读这篇文章 2. 我为什么写这篇文章 3.对齐要求 4.填充 5.结构体对齐及填充 6.结构体重排序 7.难以处理的标量的情况 8.可读性和缓存局部性 9.其他封装的技术 10.工具 ...

  10. linux 版本家族

    1. 简单的说,在桌面系统上,可分为Debian和RedHat两大分支,然后Debian这一分支到现在比较火的是Ubuntu, RedHat比较火的是Fedora.贴一下它们的版本历史:  fedor ...