[LeetCode] Contains Duplicate & Contains Duplicate II
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;
}
};
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的更多相关文章
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...
- Leetcode:面试题55 - II. 平衡二叉树
Leetcode:面试题55 - II. 平衡二叉树 Leetcode:面试题55 - II. 平衡二叉树 Talk is cheap . Show me the code . /** * Defin ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- 仿饿了么增加购物车旋转控件 - 自带闪转腾挪动画 的button
本篇文章已授权微信公众号 guolin_blog (郭霖)独家公布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/54235736 本文出 ...
- 〖Android〗快速部署SSHD和Bash Shell(程序:DroidSSHD和BetterTerminalEmulatorPro)
--此文仅做个人配置记录-- 因为我经常需要sshd来连接设备,它抓取logcat日志太方便了,方便排查问题,及多人共享: 及有USB孔限制的人来说,这个更具有意义: 把超级终端增强包部署到内网,也是 ...
- 用RotateDrawable实现网易云音乐唱片机效果
imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="唱片机" title=""> ...
- V-rep学习笔记:视觉传感器2
视觉传感器的属性设置栏中还有如下几个选项: Ignore RGB info (faster): if selected, the RGB information of the sensor (i.e. ...
- 12、java5锁java.util.concurrent.locks.Lock之ReentrantLock
JDK文档描述: public interface LockLock 实现提供了比使用 synchronized 方法和语句可获得的更广泛的锁定操作.此实现允许更灵活的结构,可以具有差别很大的属性,可 ...
- 6、redis之使用spring-data-redis的Template
POM: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- python之实现ftp上传下载代码(含错误处理)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之实现ftp上传下载代码(含错误处理) #http://www.cnblogs.com/kait ...
- 【转】IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段
背景知识 IP地址 IP地址被用来当做Internet上的电脑的身份编号.大家日常见到的情况是每台联网的PC上都需要有IP地址,才能正常通信.我们可以把“个人电脑”比作“一台电话”,那么“IP地址”就 ...
- 【mysql】一条慢查询sql的的分析
这个是我在jobbole.com 上看到的 先给出数据表table结构 mysql> show create table tt \G *************************** 1. ...
- (原)CosFace/AM-Softmax及其mxnet代码
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/8525241.html 论文: CosFace: Large Margin Cosine Loss fo ...