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的框架 docker 使用传统的client-server架构模式,用户端通过docker client 与docker  daemon 建立通信,并将请求发送给后者,而docker后端时 ...

  2. css控制超出部分自动省略...

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. spring踩坑

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...

  4. centeros 6 远程升级ssl ssh 的shell脚本

    变量说明 SSL_N=openssl-1.0.2p #ssl 版本SSH_N=openssh-7.9p1 #ssh 版本ZLIB_N=zlib-1.2.11 # zlib 版本 脚本分为两个,因为升级 ...

  5. 一篇文章掌握nightwatch自动化测试

    nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...

  6. 条款27:尽量少做转型动作(Minimize casting)

    NOTE : 1.如果可以,尽量避免转型,特别是在注重效率的代码中避免dynamic_casts. 如果有个设计需要转型动作,试着发展无需转型的替代设计. 2.如果转型是必须要的,试着将它隐藏于某个函 ...

  7. CentOS6.8下安装Docker

    原文章链接https://www.cnblogs.com/baolong/p/5743420.html. 由于在自己安装的虚拟机上打开linux终端命令行输入uname -a 以及cat /etc/r ...

  8. PHP调用webService WSDL 接口发送邮件

    1.什么是 webService WSDL?  webService WSDL 暴露一些接口给第三方调用,在底层会转化成一个HTTP请求,主要是不同语言之间为了通讯的一个协议,比如发送邮件的系统是用J ...

  9. LeetCode(237)Delete Node in a Linked List

    题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...

  10. PADS规则设计-对某一网络/元件单独设置规则

    转载请注明出处,并附带本文网址https://www.cnblogs.com/brianblog/p/9894867.html, 在PADS规则设计中可能会遇到某个走线与另一个走线之间的间距,普通规则 ...