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)的更多相关文章

  1. [leetcode] Contains Duplicate II

    Contains Duplicate II Given an array of integers and an integer k, find out whether there there are ...

  2. [LeetCode] Contains Duplicate II 包含重复值之二

    Given an array of integers and an integer k, return true if and only if there are two distinct indic ...

  3. Leetcode SingleNumber I & II & III 136/137/260

    SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...

  4. LeetCode Contains Duplicate II (判断重复元素)

    题意:如果有两个相同的元素,它们之间的距离不超过k,那么返回true,否则false. 思路:用map记录每个出现过的最近的位置,扫一边序列即可.扫到一个元素就判断它在前面什么地方出现过.本题数据有点 ...

  5. 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 ...

  6. LeetCode & Q219-Contains Duplicate II

    Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...

  7. LeetCode——Contains Duplicate II

    Description: Given an array of integers and an integer k, find out whether there there are two disti ...

  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. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

随机推荐

  1. docker存储管理

    Docker 镜像的元数据 repository元数据 repository在本地的持久化文件存放于/var/lib/docker/image/overlay2/repositories.json中 ...

  2. v8引擎详解

    引用网址: https://blog.csdn.net/swimming_in_it_/article/details/78869549 前言 JavaScript绝对是最火的编程语言之一,一直具有很 ...

  3. 把txt格式数据制作成xml数据

    txt格式数据: 代码: s1=""" <object> <name>{0}</name> <pose>Unspecifi ...

  4. vim全选复制

    网上一堆答案全是ggyG,根本不行, 正确答案应该是 gg"*yG 或者 gg"*+yG 下面是在stack overflow 上找到的答案,亲测有效,在此记录下 stackove ...

  5. VR技术在数据中心3D机房中的应用(上)

    VR技术在数据中心3D机房中的应用(上)   前两天跟朋友A吃饭,吃着吃着就说到了VR.近几年来,VR技术越来越火,感觉能跟VR沾点边的都特别高大上,朋友A也是,一提到VR,就怎么都掩盖不住他发自肺腑 ...

  6. Tunnelier使用说明

    Tunnelier与MyEnTunnel类似,但是功能更加强大.MyEnTunnel小巧易用,如何使用MyEnTunnel可以参考 MyEnTunnel使用说明 这里列下Tunnelier的优点: 1 ...

  7. [LOJ] 分块九题 7

    区间加法,区间乘法,单点查询. 洛谷线段树2 屡清加法乘法的关系,定义答案为 a*mut+add 对于整块: 新的乘w,mut和add都要乘w 新的加w,add加w //Stay foolish,st ...

  8. maven构建springmvc项目

    1.Eclipse中 NEW ->OTHER->Maven->maven project 2.选择项目路径 3.选择项目类型->next->输入groupid和artif ...

  9. ubuntu卸载编译安装的软件

    cd 源代码目录 make clean ./configure make make uninstall

  10. 年华利率n%

    年化利率12%指的是,在您出借的本金不减少的情况下,您一年后的利息将达到您出借本金的12%.也就是说,如果年化利率是12%,则每月您出借资金获得的利息是1%(12% / 12个月). 在有利网,您的投 ...