【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 ...
随机推荐
- 转载 a href=#与 a href=javascript:void(0) 的区别
a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" onCl ...
- git 使用(二)
之前写过一篇git使用(一),那是入门篇,现在的(二)可以说是进阶篇吧,主要讲一些使用过程的注意事件及相关问题的解决办法. 一.push和fetch还需要输入用户名和密码? 解决办法:看看公玥是否添加 ...
- android布局 及 布局属性
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- solr在电商平台中的使用示例简析
来源:http://blog.csdn.net/yangbutao/article/details/9450463 在电商平台中搜索是非常重要的功能,主要包括有搜索词类目导航.自动提示和搜索排序功能 ...
- 在服务器端保存ViewState
1.比较 Asp.net 的一个强大的功能就是ViewState, 但是这也成为很多人诟病的地方,我们先来看一个实例 ViewState其中一个特性就是保存页面的状态,下面我们看一个很简单的登录页面 ...
- java日期格式的转换
1.转换指定格式的日期 /** * 日期格式转换 * @throws ParseException */ public String dateformat(String date,String tab ...
- RabbitMQ用户角色及权限控制
RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator RabbitMQ各类角色描述:none不能访问 managem ...
- arranging-coins
https://leetcode.com/problems/arranging-coins/ public class Solution { public int arrangeCoins(int n ...
- poj 2096 Collecting Bugs (概率dp 天数期望)
题目链接 题意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcom ...
- openfire源码分析
启动流程 Socket接口 Socket通信使用Mina框架实现,是XMPP协议的处理入口,具体为: 消息接收后由不同的节处理器处理: StanzaHandler基础消息类型,之后进行消息路由: 最后 ...