Contains Duplicate
Total Accepted: 26477
Total Submissions: 73478 My Submissions


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)
{
set<int> result; set<int>::iterator itor ; for(int i = 0;i< nums.size();++i)
{
itor = result.find(nums[i]) ; if(itor != result.end())
{
return true;
}
else
{
result.insert(nums[i]);
}
} return false; }
};

非常简洁的解决方案,类似python 了,但是stl 中的set是基于平衡树的,而python中是hash树,所以python可能会高效一些
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
return nums.size() > set<int>(nums.begin(), nums.end()).size();
}
};

python 的版本:

class Solution:
def containsDuplicate(self, nums):
return len(nums) > len(set(nums))

c++ 的hash版本:同类的hash code是相同的,这是一个非常重要的编程思想

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> hashset;
for (int i = 0; i < nums.size(); ++i) {
if (hashset.find(nums[i]) != hashset.end()) {
return true;
}
else {
hashset.insert(nums[i]);
}
}
return false;
}
};

c++排序版本:

+2 votes
942 views
class Solution {
public:
bool containsDuplicate(vector<int>& nums)
{
int size=nums.size();
sort(nums.begin(),nums.end());
nums.erase(unique(nums.begin(),nums.end()),nums.end());
return (size!=nums.size());
}
}; +4 votes
Your running time is 28ms, if not use unique, it will be 24ms:
class Solution {
public:
bool containsDuplicate(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); ++i)
if (nums[i] == nums[i - 1])
return true;
return false;
}
};

leetcode 217 Contains Duplicate 数组中是否有重复的数字的更多相关文章

  1. leetcode 217 Contains Duplicate 数组中是否有反复的数字

     Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions Given an array o ...

  2. 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字

    题目:给定一个长度为N的数组,其中每个元素的取值范围都是1到N.判断数组中是否有重复的数字.(原数组不必保留) 方法1.对数组进行排序(快速,堆),然后比较相邻的元素是否相同.时间复杂度为O(nlog ...

  3. 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  4. 【剑指offer】找出数组中任意一个重复的数字,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 对于长度为n的数组,范围为0~n-1的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...

  5. [Leetcode 217&219]寻找数组中的重复值Contains Duplicate I & II

    [题目1] Given an array of integers, find if the array contains any duplicates. Your function should re ...

  6. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  8. LeetCode 80. 删除排序数组中的重复项 II

    LeetCode 80. 删除排序数组中的重复项 II

  9. 前端与算法 leetcode 26. 删除排序数组中的重复项

    目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...

随机推荐

  1. Vue nextTick 机制

    背景 我们先来看一段Vue的执行代码: export default { data () { return { msg: 0 } }, mounted () { this.msg = 1 this.m ...

  2. Java对象的访问定位

    java对象在访问的时候,我们需要通过java虚拟机栈的reference类型的数据去操作具体的对象.由于reference类型在java虚拟机规范中只规定了一个对象的引用,并没有定义这个这个引用应该 ...

  3. Bootstrap3 排版-页面主体

    Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428.这些属性直接赋予 元素和所有段落元素.另外,<p> (段落)元素还被设置了等于 ...

  4. python模块:网络协议和支持

    python模块:网络协议和支持 webbrowser 调用浏览器显示html文件 webbrowser.open('map.html') [webbrowser - Convenient Web-b ...

  5. python模块:时间处理模块

    http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...

  6. 在Ubuntu12.04上安装图形化配置与window共享的samba服务器

    1.安装samba图形化配置界面 sudo apt-get install system-config-samba 2.启动图形化配置界面 3.添加用户,最好是要用adduser命令去添加 具体配置可 ...

  7. 详解EBS接口开发之供应商导入

    (一)供应商常用标准表简介 1.1   常用标准表 如下表中列出了与供应商相关的表和说明: 表名 说明 其他信息 ap_suppliers 供应商头表 供应商的头信息如:供应商名.供应商编码.税号等 ...

  8. GitHub无法访问或访问缓慢解决办法

    缘由 由于众所周知的原因,Github最近无法访问或访问很慢.由于Github支持https,因此此次屏蔽Github采用的方法是dns污染,用户访问github会返回一个错误的IPFQ当然是一种解决 ...

  9. 有n个数,输出其中所有和为s的k个数的组合。

    分析:此题有两个坑,一是这里的n个数是任意给定的,不一定是:1,2,3...n,所以可能有重复的数(如果有重复的数怎么处理?):二是不要求你输出所有和为s的全部组合,而只要求输出和为s的k个数的组合. ...

  10. Java: How to resolve Access Restriction error

    Issue: Access restriction: The constructor 'BASE64Decoder()' is not API (restriction on required lib ...