[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 ...
随机推荐
- RHEL/CentOS/Fedora各种源
CentOS 默认自带 CentOS-Base.repo 源, 但官方源中去除了很多有版权争议的软件, 而且安装的软件也不是最新的稳定版. Fedora 自带的源中也找不到很多多媒体软件, 如果需要安 ...
- 关于XCode更换项目名称
1.打开项目直接修改项目名称 2.直接修改分组名 3.然后.command+B会报错 4.找到项目源文件 YourProject.xcodeproj - > 右键显示包内容->找到pro ...
- Web Service 简介
最近使用ODI的工具箱中的ODIInvokeWebService.因此简单了解下WebService的理论知识. 一.Web Service 简介 Web Service就是可编程的URL,使用标准的 ...
- LeetCode118:Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- java 如何对由json对象构成的数组形式的字符串进行遍历?
1.情景展示 现在已知字符串为: [{"name":"微微笑","img":"http://zos.alipayobjects ...
- 〖Android〗代理与正常网络分开同步CyangenMod源码
为了同步CyanogenMod源代码,你也学会了FQ,对吗? 通常 .repo/manifest.xml 文件有Google AOSP的Project,也有Github的Project: 访问Gith ...
- 全向轮运动学与V-rep中全向移动机器人仿真
Wheeled mobile robots may be classified in two major categories, omnidirectional and nonholonomic. O ...
- XPages访问关系型数据库技术与最佳实践
XPage 对于 Domino 开发人员的一大好处就是能够很方便和高效的访问关系型数据库.本文通过实例代码展现了在 XPage 中访问关系型数据库的具体步骤 , 同时讲解了一些在 XPage 中高效访 ...
- 【组队赛三】-D 优先队列 cf446B
DZY Loves Modification Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- Add Microsoft SQL JDBC driver to Maven(转)
from:http://claude.betancourt.us/add-microsoft-sql-jdbc-driver-to-maven/ Add Microsoft SQL JDBC driv ...