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. CPP-基础:快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...

  2. POJ-1200-Crazy Search(字符串Hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 Descrip ...

  3. mysql 报错Authentication method 'caching_sha2_password' is not supported

    原文地址:https://blog.csdn.net/u011583336/article/details/80999043 之前工作中用的数据库多是ms sqlserver,偶尔用到mysql都是运 ...

  4. kvm虚拟机的克隆以及快照

    克隆(常见有3种方法) 1 直接克隆(克隆虚拟机使用自己的磁盘) virt-clone -o vm-01 -n vm-02 -f /kvm/os/vm-02.qcow2 virsh start vm- ...

  5. Go:sync.Once 实现单例模式

    代码: package main import ( "fmt" "sync" ) type Singleton struct{} var singleton * ...

  6. Java后端技术微信交流群!工作、学习、技术、资源等!期待你的加入!

    <Java后端技术>专注Java相关技术:SSM.Spring全家桶.微服务.MySQL.MyCat.集群.分布式.中间件.Linux.网络.多线程,偶尔讲点运维Jenkins.Nexus ...

  7. 条款30:透彻了解inline的里里外外(understand the ins and outs of inlining)

    NOTE: 1.将大多数inline限制在小型 被频繁调用的函数身上.这可使日后的调试过程和二进制升级(binary upgradability)更容易,也可使潜在的代码膨胀问题最小化, 使程序的速度 ...

  8. python直接赋值、深浅拷贝实例剖析

    根据数据类型分为两部分进行剖析: int.str类型      list.tuple.dict类型等 1.  int.str类型 [int类型实例] >>> import copy ...

  9. Coreldraw绘制标准波浪线

    Coreldraw中如何绘制标准波浪线? 先画一根直线,单击工具栏中的“互动式工具组”,选择“互动式变形工具”, 再在弹出的属性栏中选择“拉链变形”,在幅度和频率中分别输入波形的波峰 到波底的值.波浪 ...

  10. python024 Python3 实例

    Python3 实例 以下实例在 Python3.4.3 版本下测试通过: Python Hello World 实例 Python 数字求和 Python 平方根 Python 二次方程 Pytho ...