题目描述:

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.

Example 1:

Input: [1,2,3,1]
Output: true

Example 2:

Input: [1,2,3,4]
Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]
Output: true

要完成的函数:

bool containsDuplicate(vector<int>& nums)

说明:

1、给定一个vector,要求判断vector中包不包含重复元素。如果重复,那么返回true,如果全都是不重复的,那么返回false。

2、这道题我们可以用最笨的双重循环来做,也可以增加空间复杂度,建立set,用哈希的方法来判断有没有重复。

笔者也想过能不能用异或来做,最后觉得应该还是不太行。

最终选择了排序的方法,先快排整个vector,接着遍历一次整个vector,判断相邻元素有没有相同的,如果有就返回true,如果跑完一整个循环都没有,那么返回false。

代码十分简单,如下:

    bool containsDuplicate(vector<int>& nums)
{
sort(nums.begin(),nums.end());//排序整个vector
int s1=nums.size();
for(int i=0;i<s1-1;i++)//从第一个元素开始,到倒数第二个元素结束
{
if(nums[i+1]==nums[i])//如果有相同元素
return true;
}
return false;//如果跑完全程都没有相同的
}

上述代码实测29ms,beats 99.14% of cpp submissions。

leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)的更多相关文章

  1. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  2. 25. leetcode 217. Contains Duplicate

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

  3. LN : leetcode 217 Contains Duplicate

    lc 217 Contains Duplicate 217 Contains Duplicate Given an array of integers, find if the array conta ...

  4. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素

    83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  6. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  8. java8 stream初试,map排序,list去重,统计重复元素个数,获取map的key集合和value集合

    //定义一个100元素的集合,包含A-Z List<String> list = new LinkedList<>(); for (int i =0;i<100;i++) ...

  9. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

    一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...

随机推荐

  1. ios Https问题

    HTTPS认证过程:   ① 浏览器发送一个连接请求给安全服务器.   ② 服务器将自己的证书,以及同证书相关的信息发送给客户浏览器.   ③ 客户浏览器检查服务器送过来的证书是否是由自己信赖的 CA ...

  2. jQuery的event事件

    1.冒泡和默认行为 <div class="aa"> <div class="bb"> <div class="cc&q ...

  3. Common issue on financial information exchange (FIX) Connectivity[z]

    FIX Protocol Session Connectivity Hi guys, in this post I would like share my experience with financ ...

  4. Qt的安装和使用中的常见问题(简略版)

    对于喜欢研究细节的朋友,可参考Qt的安装和使用中的常见问题(详细版). 目录 1.引入 2.Qt简介 3.Qt版本 3.1 查看安装的Qt版本 3.2 查看当前项目使用的Qt版本 3.3 查看当前项目 ...

  5. 主键冲突异常 DuplicateKeyException

    org.springframework.dao.DuplicateKeyException Exception thrown when an attempt to insert or update d ...

  6. mysql event 入门

    delimiter | CREATE EVENT statistics_event ON SCHEDULE EVERY DAY STARTS CONCAT(CURRENT_DATE(), ' 00:0 ...

  7. 何时使用[self release]

    这样的语句   [self release]; 乍看上去让人很困惑. 从release方法本身的作用上来说,就是给self的引用技术减一,就像release对其他对象所做的一样. 一般来说,唯一用到, ...

  8. ModelSim 修改测量时间显示的单位

    经常有网友问,ModelSim仿真时,测量两个点之间的时间,想显示合适的单位怎么设置,例如下图显示的两个测量光标间的时间,是以频率为单位显示的,当希望设置为以时间显示,例如ns或者ms的时候怎么办呢? ...

  9. ASP.NET Core2使用Autofac实现IOC依赖注入竟然能如此的优雅简便

    初识ASP.NET Core的小伙伴一定会发现,其几乎所有的项目依赖都是通过依赖注入方式进行链式串通的.这是因为其使用了依赖注入 (DI) 的软件设计模式,代码的设计是遵循着“高内聚.低耦合”的原则, ...

  10. SqlerMonitor-复制

    在复制系统中因为一些配置上失误和人为的失误操作导致复制堵塞,Sqler Monitor 新增加了分析复制延迟邮件,配合复制错误监控邮件和延迟邮件,和复制元数据采集 可以在第一时间准确定位到问题,适合大 ...