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

&lt;LeetCode OJ&gt; 217./219. Contains Duplicate (I / II)的更多相关文章

  1. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  2. 217/219. Contains Duplicate /Contains Duplicate II

    原文题目: 217. Contains Duplicate 219. Contains Duplicate II 读题: 217只要找出是否有重复值, 219找出重复值,且要判断两者索引之差是否小于k ...

  3. 【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 ...

  4. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. 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 ...

  6. 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 ...

  7. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  8. 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 ...

  9. LeetCode OJ:Spiral MatrixII(螺旋矩阵II)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. dojo 五 配置dojoconfig

    官方教程:Configuring Dojo with dojoConfig例子: <-- set Dojo configuration, load Dojo --> <script& ...

  2. 【NEWS】 ADempiere发布ADempiere 3.8.0路线图【2013年7月28日】

    发布源:http://osssme.org/cms/?q=node/17 本以为ADempiere”已死“,但是看到ADempiere的WIKI上大概在从5月28日开始添加WIKI以来,经过多次更新后 ...

  3. Ruby 和 Python 分析器是如何工作的?

    你好! 我作为一名编写Ruby profiler的先驱,我想对现有的Ruby和Python profiler如何工作进行一次调查. 这也有助于回答很多人的问题:“你怎么写一个profiler?” 在这 ...

  4. 列举一些常见的系统系能瓶颈 Common Bottlenecks

    http://www.nowamagic.net/librarys/veda/detail/2408在 Zen And The Art Of Scaling - A Koan And Epigram ...

  5. 停掉一台服务器,Nginx响应慢(转载)

    测试发现的问题及解决办法 1.当后端两台IIS应用服务器都正常时,访问速度非常快,查看日志,原来一个请求,是后端两台服务器同时响应的; 2.为了模仿故障测试,停掉一台IIS应用服务器,这时再访问,请求 ...

  6. Ubuntu14.04下安装docker 1.9

    有以下几种方式: 1. 通过系统自带包安装(可能不是最新版) $ sudo apt-get update $ sudo apt-get install -y docker.io $ sudo ln - ...

  7. AESDK AE中层类型的3种取得方式

    有一部分属于类型标志,比如调节层,空对象层.用mSuites->LayerSuite7()->AEGP_GetLayerFlags去取 而灯光,文字这些信息,直接取类型即可 mSuites ...

  8. Java常量池解析与字符串intern简介

    在Java应用程序运行时,Java虚拟机会保存一份内部的运行时常量池,它区别于class文件的常量池,是class文件常量池映射到虚拟机中的数据结构. 关于class文件常量池的部分可以参考之前的博文 ...

  9. C++之extern关键字

    extern关键字 extern extern关键字的作用就是告诉编译器,它修饰的变量或者函数在别处定义. extern "C" 代码一: cppExample.h #ifndef ...

  10. SAP安装前添加虚拟网卡步骤

    添加虚拟网卡: 打开控制面版中的设备管理器 点击菜单栏上的[操作(A)] 选择[添加过时硬盘件] 选择[ 安装我手动从列表选择的硬件(高级)(M) ],点击[下一步] 选择[网络适配器],点击[下一步 ...