217. Contains Duplicate

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.

题意:判断数组中是否有重复的元素,如果没有返回false,反之返回true.

解法:先对数组进行排序,然后比较排序之后数组相邻元素。

代码如下:

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int size = nums.size();
if(size == || size == )
{
return false;
}
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i++)
{
if(nums[i-] == nums[i])
{
return true;
}
}
return false;
}
};

leetcode 217的更多相关文章

  1. leetcode——217. 存在重复元素

    leetcode--217. 存在重复元素 题目描述:给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false ...

  2. [LeetCode]-217.存在重复元素-简单

    217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false . 示例 1: 输入: [1, ...

  3. 25. leetcode 217. Contains Duplicate

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

  4. LeetCode 217. Contains Duplicate (包含重复项)

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

  5. 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 ...

  6. LN : leetcode 217 Contains Duplicate

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

  7. 2017-3-11 leetcode 217 219 228

    ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...

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

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

  9. Java实现 LeetCode 217 存在重复元素

    217. 存在重复元素 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3 ...

随机推荐

  1. OpenCV MAT基本图像容器

    参考博客: OpenCv中cv::Mat和IplImage,CvMat之间的转换 Mat - 基本图像容器 Mat类型较CvMat和IplImage有更强的矩阵运算能力,支持常见的矩阵运算(参照Mat ...

  2. [转]DB2 load参数

    本文持续更新,LOAD如何提高parallelism.LOAD SHRLEVEL CHANGE的性能提高. =========================== Every once in a wh ...

  3. VS高效开发快捷键

    Ctrl + Tab 标签切换 Ctrl + '-'向后导航 Ctrl + Shift+'-'向前导航 Ctrl +Shift +空格  提示函数参数 Ctrl +F4 退出本标签 Ctrl+F 查找 ...

  4. c# List的排序

    list 是我们常用到的数据类型,我们常常会用list去处理很多的数据.我们也常常会有这样的一个操作,就是排序sort list 所在的命名空间是System.Collections.Generic ...

  5. delphi 动态更改屏幕分辨率(转)

    一.如何动态更改屏幕分辨率 有许多小工具可以在不重新启动Windows的条件下,动态更改屏幕分辨率.你是不是也想自己动手做一个呢?请在interface段中加入下面一句 function Resolu ...

  6. Intellij IDEA +MAVEN+Jetty实现Mybatis的HelloWorld

    1 maven配置:pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  7. Mybatis原理分析之一:从JDBC到Mybatis

    1.引言 本文主要讲解JDBC怎么演变到Mybatis的渐变过程,重点讲解了为什么要将JDBC封装成Mybaits这样一个持久层框架.再而论述Mybatis作为一个数据持久层框架本身有待改进之处. 2 ...

  8. c# winform 窗体起始位置 设置

    窗体起始位置为顶部中间,WinForm居中显示: ; ; this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定 thi ...

  9. ubuntu16041,安装opencv3.1.0

    [非常感谢:http://www.linuxdiyf.com/linux/18482.html] 1.依赖关系: sudo apt-get install build-essentialsudo ap ...

  10. 异步|同步&阻塞|非阻塞

    异步|同步:区别在于发出一个功能调用时,是否马上得到返回结果 阻塞|非阻塞:区别在于调用结果返回之前,当前线程是否挂起 node.js:单线程.异步非阻塞模型 单线程与异步不矛盾,与并发是矛盾的 ht ...