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.

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> st;
for (auto n : nums) {
if (st.find(n) != st.end()) return true;
else st.insert(n);
}
return false;
}
};

Contains Duplicate II

Given an array of integers and an integer k, return true if and only if 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.

 class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int res = false;
unordered_map<int, int> mp;
for (int i = ; i < nums.size(); ++i) {
if (mp.find(nums[i]) != mp.end()) {
res = true;
if (i - mp[nums[i]] > k) return false;
} else {
mp[nums[i]] = i;
}
}
return res;
}
};

两题都是简单的hash表的应用。

[LeetCode] Contains Duplicate & Contains Duplicate II的更多相关文章

  1. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  4. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  5. Leetcode:面试题68 - II. 二叉树的最近公共祖先

    Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...

  6. Leetcode:面试题55 - II. 平衡二叉树

    Leetcode:面试题55 - II. 平衡二叉树 Leetcode:面试题55 - II. 平衡二叉树 Talk is cheap . Show me the code . /** * Defin ...

  7. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  8. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  10. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. 【shell】各种括号()、(())、[]、[[]]、{}的使用

    圆括号 1.单圆括号() ①命令组,括号中的命令将会开启一个子shell独立运行:括号中以分号连接,最后一个命令不需要;各命令和括号无需空格 Linux:/qins # (var=1;echo $va ...

  2. 【DB2】建造测试数据

    建表语句 CREATE TABLE     FUND_DAILY_INCOME     (         ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTIT ...

  3. C#与Java同步加密解密DES算法

    在实际项目中,往往前端和后端使用不同的语言.比如使用C#开发客户端,使用Java开发服务器端.有时出于安全性考虑需要将字符加密传输后,由服务器解密获取.本文介绍一种采用DES算法的C#与Java同步加 ...

  4. 【laravel5.4】php artisan 常用命令

        路由缓存:/www/wd***/php/bin/php artisan route:cache 查看全部路由并输出到txt文件:/www/wd***/php/bin/php artisan r ...

  5. (原+转)win7上编译caffe支持python及matlab

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7126126.html 参考网址: https://github.com/happynear/caffe ...

  6. JavaScript RegExp Object 正则表达式入门

    什么是 RegExp? RegExp 是regular expression的缩写. RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 当您检索某个文本时,可以使用一种模式来描述 ...

  7. 封装naive socket

    周五去一个公司打了个酱油,面试官问我:你封装过socket没? 言下之意是问我实际写过底层代码没,我悻悻地说写过点. PS:说实话木有封装过,今天无聊就来封装下. 话说写了这么久C++,底层用c来写还 ...

  8. 遇到影响服务器性能的cpuspeed 服务

    最近碰到一个很蛋痛的问题,,我在公司的代码上实现了一个功能,然后基于这个测试,结果比对数据发现每天少三千多万条,, 然后我各种优化,各种零碎部功能阉割,,还是丢数据! 之后,监控运行网卡----wat ...

  9. webdriver之py,driver启动chrome时加载profile

    import os from selenium import webdriver from selenium.webdriver.chrome.options import Options execu ...

  10. MachineLearning之Logistic回归

    一.概述 假设现在有一些数据点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称为回归: 利用Logistic回归进行分类的主要思想是: 根据现有数据对分类边界线建立回归公式, ...