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. tensorflow共享变量 the difference between tf.Variable() and get_variable()

    一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...

  2. 搭建 RabbitMQ Server 高可用集群

    阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...

  3. Python:操作数据库

    (一)      前言 本文说明如何连接Oracle.MySQL.sqlserver,以及执行sql.获取查询结果等. (二)      DB-API      DB-API阐明一系列所需对象和数据库 ...

  4. 解析配置文件redis.conf

    units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...

  5. compress函数用法详解

    compress函数: 主要用来删除字符串中的特定字符. 1.compress函数的基本形式compress(<source><, chars><, modifiers& ...

  6. Node.js URL

    稳定性: 3 - 稳定 这个模块包含分析和解析 URL 的工具.调用 require('url') 来访问模块. 解析 URL 对象有以下内容,依赖于他们是否在 URL 字符串里存在.任何不在 URL ...

  7. JDK、JRE和JVM的关系

    JDK中包含了JRE,JRE中包含了JVM. 详解: JDK是JAVA的核心,包括JRE(JAVA 虚拟环境).编译器等,JDK的主流产品是由SUN公司开发的,JDK本身是用JAVA编写的,安装包的S ...

  8. Dubbo框架应用之(三)--Zookeeper注册中心、管理控制台的安装及讲解

    我是在linux下使用dubbo-2.3.3以上版本的zookeeper注册中心客户端.Zookeeper是Apache Hadoop的子项目,强度相对较好,建议生产环境使用该注册中心.Dubbo未对 ...

  9. Velocity 语法及其在springMVC中的配置

    强烈推荐具体的整合博客:http://blog.csdn.net/duqi_2009/article/details/47752169 整合文章中有几处问题: xml中配置的vm视图解析器,应该按照本 ...

  10. 一个整数数组,长度为n,将其分为m份,使各份的和相等,求m的最大值。

    比如{3,2,4,3,6} 可以分成 {3,2,4,3,6} m=1; {3,6}{2,4,3} m=2 {3,3}{2,4}{6} m=3 所以m的最大值为3. bool isShare(int* ...