【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - 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.
Solution 1: sort then compare the adjacent number
class Solution {
public:
bool containsDuplicate(vector<int>& nums) { //runtime:40ms
if(nums.size()<=)return false;
sort(nums.begin(),nums.end());
for(int i=;i<nums.size();i++){
if(nums[i-]==nums[i])return true;
}
return false;
}
};
Solution 2: map记录已存在的int
class Solution {
public:
bool containsDuplicate(vector<int>& nums) { //runtime:104ms
if(nums.size()<=)return false;
map<int,bool> m;
for(int i=;i<nums.size();i++){
if(m[nums[i]]==true)return true;
m[nums[i]]=true;
}
return false;
}
};
219 - Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
map<int,int> m;
for(int i=;i<nums.size();i++){
if(m.find(nums[i])==m.end())
m[nums[i]]=i;
else{
if(i-m[nums[i]]<=k)
return true;
else
m[nums[i]]=i;
}
}
return false;
}
};
【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II的更多相关文章
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【leetcode】955. Delete Columns to Make Sorted II
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】217. Contains Duplicate 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计词频 使用set 排序 日期 [LeetCo ...
- 【LeetCode】217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- 【LeetCode】217.存在重复元素
217. 存在重复元素 知识点:数组:Set: 题目描述 给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 fals ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
随机推荐
- Highgui.imwrite("/data/pic1111.png", mat)失败,且找不到报错
——> Highgui.imwrite("/data/pic1111.png", mat)失败,且找不到报错. ok -->直接使用以下代码,自己保存mat,从而可以跟 ...
- PostgreSQL的创建表
PostgreSQL的CREATE TABLE语句是用来在任何指定的的数据库中创建一个新表. 语法 CREATE TABLE语句的基本语法如下: CREATE TABLE table_name( co ...
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- bigtint;int;smallint;tinyint
bigint对应的是Int64 [long] int对应的是Int32 [int] smallint对应的是Int16 [short] tinyint对应的是 [byte ...
- 谈谈Perforce
实习就要结束了,收获之一就是学会了使用Perforce! Perforce SCM System是一款构建于可伸缩客户/服务器结构之上的软件配置管理工具.仅仅应用 TCP/IP,开发人员就能够通过多种 ...
- 纠结和郁闷的存在感-关于DirectX与HLSL的矩阵存储方式---转载好文章
我常常这么大胆的认为,搞科学的人总是喜欢用各种让常人难以理解的复杂方式去处理某些其实可能很简单的事情,这种情况在他自身擅长的.有着诸多对手竞争的领域上极为常见.比如吧,搞DirectX的人用了左手坐标 ...
- Android 第三方应用接入微信平台(2)
微信平台开放后倒是挺火的,许多第三方应用都想试下,毕竟可以利用微信 建立起来的关系链来拓展自己的应用还是挺不错的,可以节约很多在社交方 面的开销,我最近由于实习需要也在研究这个东西,不过发现网上的相关 ...
- 多态and接口
一.多态 1.什么是多态? 解析:不同的对象对于同一个操作,做出的响应不同 具有表现多种形态的能力的特征 2.使用多态的优点 解析:为了实现统一调用 一个小例子:<父类类型作为参数> 父类 ...
- div中文字水平和垂直居中的css代码
HTML元素 <div>水平垂直居中</div> css样式 div{ width:200px;height:200px; /*设置div的大小*/ border:1px so ...
- HDU 1244 Max Sum Plus Plus Plus
虽然这道题看起来和 HDU 1024 Max Sum Plus Plus 看起来很像,可是感觉这道题比1024要简单一些 前面WA了几次,因为我开始把dp[22][maxn]写成dp[maxn][2 ...