Contains Duplicate,Contains Duplicate II,Contains Duplicate III
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的更多相关文章
- Remove Duplicate Letters I & II
Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...
- [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 ...
- LeetCode 219: 存在重复元素 II Contains Duplicate II
题目: 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. Given an ...
- 查找表 219.Contains Duplicate(2),217 Contain Duplicate, 220(3)
思路:滑动窗口(长度为k+1)看这个窗口里的是否有两个元素的值相同.加查找表. //时间:O(n) //空间:O(k) class Solution { public: bool containsNe ...
- 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 ...
- [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 ...
- Best Time to Buy and Sell Stock I && II && III
题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- RMAN_学习实验2_RMAN Duplicate复制数据库过程(案例)
待整理 对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rm ...
随机推荐
- Python同时向控制台和文件输出日志logging的方法 Python logging模块详解
Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...
- 层次查询start with ... connect by
如:select distinct dep_id from t_sys_dep_dimensions start with dep_id = (select dept_id from t_sys_pe ...
- javascript事件详细说明
javascript事件列表解说javascript事件列表解说事件 浏览器支持 解说一般事件 onclick IE3.N2 鼠标点击时触发此事件ondblclick IE4.N4 鼠标双击时触发此事 ...
- shopnc B2B2C商城 Nginx下开启伪静态
B2B2C商城 Nginx下开启伪静态,伪静态开启后,对系统的SEO极为有利,可以最大限度让商城页面被搜索引擎抓取,但在实际安装中,很多客户在这块都会遇到各种各样的问题. 1. 编辑商城配置文件(da ...
- USB 调试工具(python2.7 + Tkinter + pyusb/pywinusb)
项目地址:USB-HID-TEST 整体预览图(win8下的效果): ====================== 项目结构: COM --hidHelper.py --usbHelper.py UI ...
- 容器 MAP
1.equal_range pair <myMapDef::iterator,myMapDef::iterator> myresult; myPairDef ps=*MyMap1.begi ...
- hdu Largest prime factor
类似于素数打表. #include <cstdio> #include <cstring> #include <algorithm> #define maxn 10 ...
- PYTHON线程知识再研习F---队列同步Queue
让我们考虑更复杂的一种场景:产品是各不相同的.这时只记录一个数量就不够了,还需要记录每个产品的细节.很容易想到需要用一个容器将这些产品记录下来. Python的Queue模块中提供了同步的.线程安全的 ...
- 中国IC业“芯”结:IC小国真能赶追韩美日么?
集成电路是关系到国民经济和社会发展的战略性.基础性和先导性产业,是培育发展战略性新兴产业.推动信息化和工业化深度融合的核心与基础.因此,我 国历来就十分重视集成电路产业的培育和发展,在这方面投入了大量 ...
- 简单工厂模式 - OK
简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现. 使用场景: 工厂类负责创建的对象比较少: 客户只知道传 ...