217/219. Contains Duplicate /Contains Duplicate II
原文题目:
读题:
217只要找出是否有重复值,
219找出重复值,且要判断两者索引之差是否小于k
//217. Contains Duplicate 46ms
class Solution
{
public:
bool containsDuplicate(vector<int>& nums)
{
set <int> s;
//vector <int>::iterator it;
int i = 0;
if(nums.empty())
{
return false;
}
s.insert(nums[0]);
for(i = 1;i < nums.size();++i)
{
if(s.count(nums[i]))
{
return true;
}
s.insert(nums[i]);
}
return false;
}
}; //217. Contains Duplicate 29ms
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
std::sort(nums.begin(), nums.end());
int i = 0, j = nums.size() - 1;
while (i < j) {
if (nums[i] == nums[i+1])
return true;
++i;
}
return false;
}
}; //219. Contains Duplicate II 25ms
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map <int, int> m; //如果用的是map,那么时间复杂度为35ms
for (int i = 0; i < nums.size(); ++i)
{
if (m.find(nums[i]) != m.end() && i - m[nums[i]] <= k) return true;
else m[nums[i]] = i;
}
return false;
}
};
217/219. Contains Duplicate /Contains Duplicate II的更多相关文章
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II
[题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...
- <LeetCode OJ> 217./219. Contains Duplicate (I / II)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- Remove Duplicate Letters I & II
Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- [LeetCode] Contains Duplicate & Contains Duplicate II
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 2017-3-11 leetcode 217 219 228
ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
随机推荐
- 互斥锁,IPC队列
进程同步(锁) 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,part1:共享同一打印终端,发现会有多行内容打印到一行的现象(多个进程共享并抢占同一个 ...
- CentOS7.4安装部署openstack [Liberty版] (一)
一.OpenStack简介 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是一个 ...
- linux:Apache服务器相关
安装apache服务器 (推荐方法) 安装前清确保已安装gcc.g++ 1)安装依赖包apr-1.6.5.tar.gz,下载地址:http://apr.apache.org/download.cgi ...
- es6基础(3)-正则扩展
//正则扩展 { let regex=new RegExp('xyz','i'); let regex2=new RegExp(/xyz/i); console.log(regex.test('xyz ...
- 【Jmeter自学】Jmeter里的指标
聚合报告 Average:平均响应时间(毫秒ms) Median:中值时间,N个数据从小到大排列,第N/2个数 9x%Line:N个数据从小到大排列,第9x%N个数.所有数据中9x%的响应时间都小于9 ...
- unicode转中文以及str形态的unicode转中文
今天在工作中遇到这样一个问题(工作环境为Python2.7.1),需要将一个字典中字符串形态的Unicode类型的汉字转换成中文,随便总结一下: 1.unicode转中文 old = u'\u4e2d ...
- PHP中json_encode()问题
PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 要想不转义,在后面加个参数即可 json_encode($data, JSON_UNESCAPED_UNICODE); ...
- [UGUI]滑动列表优化(循环利用)
需要注意的有下面几点: 1. 区分好表现上的index和逻辑上的index.表现上的index是指这个go是go列表中的第几项,但实际上这个index的意义并不大,因为在滚动的过程中go列表是轮转的: ...
- Windows下python库的常用安装方法
目录: 1.pip安装(需要pip) 2.通过下载whl文件安装(需要pip) 3.在pythn官网下载安装包安装(不需要pip) 方法一:pip安装. 这是最 ...
- 24.纯 CSS 创作出平滑的层叠海浪特效
原文地址:https://segmentfault.com/a/1190000014895634 感想:这里的波浪只是侧面的,利用几个平面一部分弧旋转得到. HTML代码: <div class ...