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. 【Zookeeper】源码分析之持久化(二)之FileSnap

    一.前言 前篇博文已经分析了FileTxnLog的源码,现在接着分析持久化中的FileSnap,其主要提供了快照相应的接口. 二.SnapShot源码分析 SnapShot是FileTxnLog的父类 ...

  2. JqGrid把数据行插入指定位置的方法addRowData

    1.首页在colModel里写好方法,如下代码options.rowId是获取当前行的编号 { label: '操作', width: 60, align: 'center', formatter: ...

  3. Oracle11g口令过期的解决

    今天发现服务器上的Oracle11g突然登录不上去了,提示ORA-28002错误,说是口令过期. 1. 用DBA账户登录SQL PLUS.我用的是SYS. 2. 系统会提示口令失效,但是会马上让你重置 ...

  4. 源代码解析Android中View的layout布局过程

    Android中的Veiw从内存中到呈如今UI界面上须要依次经历三个阶段:量算 -> 布局 -> 画图,关于View的量算.布局.画图的整体机制可參见博文 < Android中Vie ...

  5. RHEL7 光盘修复

    光盘修复#rm -rf /boot/*#dd if=/dev/zero of=/dev/sda bs=446 count=1#systemctl rebootTroubleshooting(排错模式) ...

  6. 转:CMake快速入门教程-实战

    CMake快速入门教程:实战 收藏人:londonKu     2012-05-07 | 阅:10128  转:34    |   来源   |  分享               0. 前言一个多月 ...

  7. ios中PagedFlowView的用法

    下载地址 引入PagedFlowView.h  PagedFlowView.m文件 #import <UIKit/UIKit.h> #import "PagedFlowView. ...

  8. 大道至简的C语言内存管理

    C语言内存的开辟和释放需要程序员自己来实现. 而C语言的内存开辟和释放只在stdlib.h里面提供了四个函数,这么简洁的设计就足以完成一切工作. C++里面各种类型的指针漫天飞舞,显得啰嗦冗余. ca ...

  9. 封装naive socket

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

  10. IP首部格式[转载]

    TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程:   来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...