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.

思路:只要一个整数数组中某个元素出现两次及以上,返回true,否则返回false。将数组进行排序,只要找到出现两次的元素就可以了。

另外,需要注意的是,找到出现两次的元素的思想是,nums[i]=nums[i+1],这样会忽略数组长度为0或者1的情况,因此应该将这两种情况单独列出来。

 bool containsDuplicate(vector<int>& nums) {
if(nums.empty())
return false ;
int n = nums.size();
if(n == )//或者将这里写成if(n == 0 || n == 1)
return false;
sort(nums.begin(), nums.end());
for(int i = ; i < n; i++){
if(nums[i] == num[i+])
return true;
}
else if (i == n-)
return false;
}

[Array]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. 217. Contains Duplicate(C++)

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

  3. 25. leetcode 217. Contains Duplicate

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

  4. 217. Contains Duplicate【easy】

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

  5. LN : leetcode 217 Contains Duplicate

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

  6. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  7. LeetCode Array Easy 217. Contains Duplicate

    Description Given an array of integers, find if the array contains any duplicates. Your function sho ...

  8. leetcode 217 Contains Duplicate 数组中是否有重复的数字

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

  9. 【LeetCode】217. Contains Duplicate (2 solutions)

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

随机推荐

  1. l洛谷 NOIP提高组模拟赛 Day2

    传送门 ## T1 区间修改+单点查询.差分树状数组. #include<iostream> #include<cstdio> #include<cstring> ...

  2. 洛谷P3376【模板】网络最大流  Dinic模板

    之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...

  3. Excel 2016在大数据分析领域有了很多的改善

    Excel 2016在大数据分析领域有了很多的改善 通常,我们会把大数据分析的整个过程分为五个阶段: 获取获取,数据分析,可视化,发布报告,应用报告. 在获取数据方面,Excel 2016相对Exce ...

  4. Linux 启动、关闭、重启服务的两种方式

    1.一种是可以使用service脚本来调度,如: service 服务名 startservice 服务名 stopservice 服务名 restart 2.第二种可以直接进入/etc/init.d ...

  5. rabbit mq 基础流程(转)

    从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件    从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...

  6. RaspberryPi(一)

    [1]格式化TF卡 // 注意格式 [2]烧录系统 // 烧录完成后不要点弹出的击格式化选项 [3]查找IP.修改静态IP(保持和台式机或笔记本同网段) arp -a //物理地址以B8开头 //或者 ...

  7. [转]WPF中的动画

    WPF中的动画                                                                                  周银辉 动画无疑是WP ...

  8. day45作业

    利用 html + css 写一个最基本的页面 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. C语言作用域、链接属性和存储类型

    C/C++中作用域详解 作用域 编译器可以确认的4种作用域-代码块作用域.文件作用域.函数作用域和原型作用域,一般来说,标识符(包括变量名和函数名)声明的位置决定它的作用域. (1)代码块作用域 一对 ...

  10. Ionic 选择图片上传

    1.添加插件 1.1 安装ngcordova 1.2 安装选择图片插件 1.3 安装上传插件 1.4查看安装插件集合 2.html 代码 <div class="item item-i ...