题目描述:

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. jQuery的选择器的总结

    一.简单选择器 // $(function () { // $("#box").css("color","red") // }) // 这个 ...

  2. How to Restart Qt Application

    How to restart QtApplication As we know, Restarting Application means to exit current application, t ...

  3. 理解数据库中的undo日志、redo日志、检查点

    数据库存放数据的文件,本文称其为data file. 数据库的内容在内存里是有缓存的,这里命名为db buffer.某次操作,我们取了数据库某表格中的数据,这个数据会在内存中缓存一些时间.对这个数据的 ...

  4. [SoapUI] 通过Groovy Script获取当前运行的是哪套Environment

    log.info testRunner.testCase.testSuite.project.getActiveEnvironment().getName()

  5. oracle存储过程和游标参考

    oracle open cursor forhttp://www.itpub.net/thread-1874683-1-1.html

  6. 使用junit单元测试SpringMvc

    对于有依赖关系的方法,junit测试会有些麻烦,可以用@before @after之类的创建数据库连接,然后进行测试,但是有些太麻烦了. 所以就使用一下这个:org.springframework.t ...

  7. 20155311 2016-2017-2 《Java程序设计》第8周学习总结

    20155311 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 通用API: •日志API • 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用 ...

  8. Android Studio修改默认Activity继承AppCompatActivity

    在Android Studio中新建Activity默认继承AppCompatActivity,感觉这点十分不爽,找了很久,终于发现在android Studio安装目录下有个模板文件,修改其中的参数 ...

  9. Redis Quick Start [熟练版]

    一.下载解压 wget http://download.redis.io/redis-stable.tar.gztar xvzf redis-stable.tar.gzcd redis-stable ...

  10. Solr 从文件创建索引

    http://blog.csdn.net/clj198606061111/article/details/21492457 http://wiki.apache.org/solr/Extracting ...