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. arm-linux 裸机下 VNC 的实现

    这里的 arm-linux 裸机指的是,只有基本 C 库和安装了 busybox 的嵌入式系统,没有 X11 或者 wayland 作为底层支援. 这里的实现是基于 framebuffer 的,是将用 ...

  2. 朴素贝叶斯分类器(Naive Bayes)

    1. 贝叶斯定理 如果有两个事件,事件A和事件B.已知事件A发生的概率为p(A),事件B发生的概率为P(B),事件A发生的前提下.事件B发生的概率为p(B|A),事件B发生的前提下.事件A发生的概率为 ...

  3. emplace_back() 和 push_back 的区别(转)

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  4. Web Service 简介

    最近使用ODI的工具箱中的ODIInvokeWebService.因此简单了解下WebService的理论知识. 一.Web Service 简介 Web Service就是可编程的URL,使用标准的 ...

  5. mysql日期/时间转换为字符串

    将日期时间2016-05-13 16:07:50转化为字符串20160513 date_format select phone,  date_format(time, '%Y%m%d%H%i%s') ...

  6. django之创建第11个项目-页面整合

    目的:将如下众多html页面整合到一个index.html页面中. 百度云盘:django之创建第11个项目-页面整合 用下面的方式实现: <!DOCTYPE html> <head ...

  7. Web工程中各类地址的写法

    1)总体原则 在java web开发中,只要是url地址,那么最好以“/”开头,也就是绝对路径的方式.那么这个“/”到底代表什么呢? 如果“/”是给服务器用的,则代表当前web工程:如果是给浏览器用的 ...

  8. SpringMVC 参数中接收数组、List写法

    本文使用SpringMVC版本: org.springframework:spring-web:4.3.9.RELEASE 写法及说明(示例代码的类上的注解是@RestController,所以不需要 ...

  9. Jenkins执行批处理文件失败

    今天搭建Jenkins持续集成环境,编译环境是.net,在.net下没有比较好的代码覆盖率测试插件,所以用了开源的OpenCover,计算代码覆盖率,然后用ReportGenerator导出hmtl格 ...

  10. python实现的摩斯电码解码\编码器

    代码地址如下:http://www.demodashi.com/demo/14743.html 详细说明: 现在这年头谍战片.警匪片动不动就用摩斯密码来传递信息,一方面可以用来耍帅,另外一方面好像不插 ...