[LeetCode] Contains Duplicate(II,III)
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.
解题思路
用一个set保存数组中的值,假设发现当前值已经在set中存在。则返回true。
实现代码
// Rumtime: 67 ms
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> s;
for (int i = 0; i < nums.size(); i++)
{
if (s.insert(nums[i]).second == false)
{
return true;
}
}
return false;
}
};
Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
解题思路
用map存储数组元素和下标。看是否存在与当前元素相等且下标之差小于等于k的元素,存在则返回true。否则将当前元素和其下标存入map。
实现代码
// Runtime: 76 ms
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int, int> mymap;
for (int i = 0; i < nums.size(); i++)
{
if (mymap.find(nums[i]) != mymap.end() && i - mymap[nums[i]] <= k)
{
return true;
}
else
{
mymap[nums[i]] = i;
}
}
return false;
}
};
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.
解题思路
用一个multiset存储数组中的元素,保持multiset中元素个数为k。这样multiset中元素与当前元素num[i]的下标之差均小于等于k。
iterator lower_bound (const value_type& val) const
能够用于返回值小于等于val的元素的迭代器,推断返回迭代器所指向的值与当前元素之差是否小于等于t就可以。
实现代码
// Runtime: 44 ms
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
multiset<int> s;
for (int i = 0; i < nums.size(); i++)
{
if (s.size() == k + 1)
{
s.erase(s.find(nums[i-k-1]));
}
auto it = s.lower_bound(nums[i] - t);
if (it != s.end())
{
int diff = nums[i] > *it ? nums[i] - *it : *it - nums[i];
if (diff <= t)
{
return true;
}
}
s.insert(nums[i]);
}
return false;
}
};
[LeetCode] Contains Duplicate(II,III)的更多相关文章
- [leetcode] Contains Duplicate II
Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- Leetcode SingleNumber I & II & III 136/137/260
SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...
- LeetCode Contains Duplicate II (判断重复元素)
题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...
- leetcode Contains Duplicate II python
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode & Q219-Contains Duplicate II
Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...
- LeetCode——Contains Duplicate II
Description: Given an array of integers and an integer k, find out whether there there are two disti ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
随机推荐
- 制作JPEGImages出现的bug
我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...
- 光猫&路由器网络配置
前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...
- Sqlserver添加加字段、删除字段、修改字段类型、修改字段名、修改字段默认值
参考:https://www.cnblogs.com/pangpanghuan/p/6432331.html 初始化表: --.添加字段 --1.1.为null alter table DataTab ...
- Java 对象的创建以及类加载
1. 对象的创建的过程: 类加载检查—>分配内存—>初始化零值—>设置对象头—>执行 init . 1.类加载检查: 虚拟机遇到一条 new 指令时,首先将去检查这个指令的参数 ...
- (1) openssl基础概念
1.1 背景知识 对称加密 :加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2. 非对称加密 :使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方 ...
- c++_等差素数列
标题:等差素数列 2,3,5,7,11,13,....是素数序列.类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列.上边的数列公差为30,长度为6. 2004年 ...
- 第六天,字典Dictionary
字典(Dictionary)在Python中是一种可变的容器模型,它是通过一组键(key)值(value)对组成,这种结构类型通常也被称为映射,或者叫关联数组,也有叫哈希表的.每个key-value之 ...
- winform中ComboBox控件的简单使用
在开发winform中用到了ComboBox,但是发现和asp.net中的DropDownList差别比我想象中的大. 给ComboBox添加数据总结的有两种方法(绑定数据库在这里不说): 第一种方法 ...
- mq推送消息
场景:BDM(实名制系统)同步数据到CRNS(实名制系统) 一,首先建个队列,队列名字为 bdm_empolyeeinfo_crns 二,applicationContext-rabbitmq.xml ...
- 五、PL/SQL循环、游标、函数和过程
--PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...