<LeetCode OJ> 217./219. Contains Duplicate (I / 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.
第一种方法:set数据结构,count函数记录数是否出现过,耗时96ms
用数据结构set来做。由于他是红黑树为底层,所以查找某个元素时间浮渣度较低,而且set不同意同样元素出现
假设某个元素的count为0则。插入到set,假设不为0,return false
set的查找时间为O(lg(N))所以终于时间浮渣度为O(Nlg(N))
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false;
set<int> s;
s.insert(nums[0]);
for(int i=1;i<nums.size();i++)
{
if(!s.count(nums[i]))
s.insert(nums[i]);
else
return true;
}
return false;
}
};
或者set直接查找,100ms:
//思路首先:set查找
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> st;
//for (auto i : nums) {
for(int i=0;i<nums.size();i++)
{
if (st.find(nums[i]) != st.end())
return true;
st.insert(nums[i]);
}
return false;
}
};
另外一种方法:map数据结构,count函数记录数是否出现过。96ms
//思路首先:
//既然能够用set来做,那么map来做也是能够的
//由于map不同意关键值反复(但实值能够),道理和set一样,都是红黑树为底层,本方法96ms完毕測试案例
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false;
map<int, int> mapping;
for (int i = 0; i<nums.size(); i++) {
if(mapping.count(nums[i]))//假设该数出现过
return true;
mapping.insert(pair<int, int>(nums[i], i));//将nums[i]存储为关键值,实值在这里无所谓
}
return false;
}
};
或者map直接查找,100ms:
//思路首先:map查找
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false;
map<int, int> mapping;
for (int i = 0; i<nums.size(); i++) {
if(mapping.find(nums[i]) != mapping.end())//查找该数是否出现过
return true;
mapping.insert(pair<int, int>(nums[i], i));//将nums[i]存储为关键值,实值在这里无所谓
}
return false;
}
};
第三种方法:先排序,再遍历一遍。检查数是否出现过。40ms
//思路首先:先排序。再遍历一遍是否有同样值。所以终于时间浮渣度为O(Nlg(N)+N),本方法40ms完毕測试案例
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false;
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); i++) {
if (nums[i-1] == nums[i])
return true;
}
return false;
}
};
第四种方法:哈希法。检查数是否出现过。48ms
//思路首先:hash法
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> hashset;
//for (auto i : nums) {
for(int i=0;i<nums.size();i++)
{
if (hashset.find(nums[i]) != hashset.end())
return true;
hashset.insert(nums[i]);
}
return false;
}
};
有一个数组和一个整数,推断数组中是否存在两个同样的元素相距小于给定整数k。若是则返回真。
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k.
分析:
哈希map(不要用红黑树map)
遍历数组。首先看当前元素是否在map中。如不在则压入,若在看是否其相应下标和当前下标相距为k
假设不则将原元素改动为如今的下标
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.empty())
return false;
unordered_map<int,int> umapping;
umapping[nums[0]]=0;
for(int i=1;i<nums.size();i++)
{
//if(umapping.count(nums[i]) == 0)//没有出现(统计函数)
if(umapping.find(nums[i]) == umapping.end())//直接查找
umapping[nums[i]]=i;
else if((i-umapping[nums[i]])<=k)//相距小于k
return true;
else
umapping[nums[i]]=i;
}
return false;
}
};
注:本博文为EbowTang原创。兴许可能继续更新本文。
假设转载,请务必复制本条信息。
原文地址:http://blog.csdn.net/ebowtang/article/details/50443891
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 217./219. Contains Duplicate (I / II)的更多相关文章
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- 217/219. Contains Duplicate /Contains Duplicate II
原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...
- 【LeetCode OJ】Binary Tree Level Order Traversal II
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode OJ 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode OJ:Spiral MatrixII(螺旋矩阵II)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- OpenERP Web Client设置闲置有效时间
在Web Client端使用OpenERP时,默认的cookie有效时间是浏览器的当前作业窗口,这样就是说只要你不关闭浏览器,不管闲置多长时间,当前的连线都是有效的.这样就会有安全问题,如果你忘了登出 ...
- Windows Phone ProgressRing 控件
在windows phone 8中,只有ProgressBar的控件,而没有圆环形的等待控件.今天我突发奇想,从Windows Store 的ProgressRing控件上copy下来的XAML 代码 ...
- 携程实时大数据平台演进:1/3 Storm应用已迁到JStorm
携程大数据平台负责人张翼分享携程的实时大数据平台的迭代,按照时间线介绍采用的技术以及踩过的坑.携程最初基于稳定和成熟度选择了Storm+Kafka,解决了数据共享.资源控制.监控告警.依赖管理等问题之 ...
- 【mysql】主键、普通索引、唯一索引和全文索引的比较
YSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录 开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...
- Java注解总结
注解是Spring和Mybatis框架所大量使用的技术,要想掌握框架相关技术,注解是必须要掌握的. 掌握注解的优势: 1.能够读懂别人写的代码,特别是框架相关的代码. 2.本来可能需要很多配置文件,需 ...
- Dapper 中使用sql in 关键字查询
传统 sql in 写法是 SELECT * FROM dbo.Users s WHERE s.id IN (1,2,3) 在dapper因为安全性,不能直接用sql接接 要采用参数化, 开始我 ...
- sqlserver学习笔记(五)—— 查询
查询的基本语法结构: select 列名/* from 表名 [where 限制条件] [order by 排序] 1.查询全部的行和列: select * from 表名 2.查询部分行和列:(部分 ...
- 非super user管理会话
在gp中取消或者中断某个用户的超长时间或者SQL存在问题的会话.假设无法拥有超级用户将无法运行该类操作. 首先我们创建两个用户t1.t2,而且使用t1登录到数据库. [gpadmin@wx60 ~ ...
- atitit.资源释放机制--attilax总结
atitit.资源释放机制--attilax总结 1. .全手工, 1 2. 引用计数, 1 2.1. 成本也显而易见. 1 2.2. 循环引用的问题, 2 2.3. 引用计数方式事实上也有经典的卡顿 ...
- Centos7 通过yum源安装nginx
通过rpm 添加yum源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ng ...