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. 使用CEfSharp之旅(4)cefsharp 调用F12

    原文:使用CEfSharp之旅(4)cefsharp 调用F12 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https://bl ...

  2. CDH断电后 hbase出现spilt块不完整问题

    从错误看起来是regionspilt时候断电了,导致hbase master启动不起来,因为是测试环境只能删除这些region了,掉一部分数据 删除hbase下spilt块,删除zK里面的habse ...

  3. Python基础知识之4——三大控制结构

    控制结构就是控制程序执行顺序的结构. Python 有三大控制结构,分别是顺序结构.分支结构(选择结构)以及循环结构.任何一个项目或者算法都可以使用这三种结构来设计完成.这三种控制结构也是结构化程序 ...

  4. C++ AOP手法

    1.代理模式 2.模版 3.NVI(non-virtual interface) 参考:https://www.cnblogs.com/qicosmos/p/4772389.html <effe ...

  5. HZOI20190816模拟23 mine/water/gcd

    A:mine 只是一个简单的dp....是博主太蒻了... 设f[i][j],表示到第i位,状态是j的方案数,其中$j\in[0,5]$ j==0表示填0,j==1表示填1,且i-1位是雷; j==2 ...

  6. Redis深度历险——核心原理与应用实践

    高可用架构」的各位老铁们,你们好!你是否还记得上个月发布的文章中,有两篇深入讲解Redis的文章,分别是和,广大粉丝读者们对这两篇文章整体评价颇高.而我就是这两篇文章的原创作者「老钱」(钱文品),我是 ...

  7. System.Web.Mvc.ContentResult.cs

    ylbtech-System.Web.Mvc.ContentResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Publ ...

  8. 杂项-公司:Amazon

    ylbtech-杂项-公司:Amazon 亚马逊公司(Amazon,简称亚马逊:NASDAQ:AMZN),是美国最大的一家网络电子商务公司,位于华盛顿州的西雅图.是网络上最早开始经营电子商务的公司之一 ...

  9. String类的trim() 方法

    用于删除字符串的头尾空白符. 语法:public String trim() 返回值:删除头尾空白符的字符串. public class Test {    public static void ma ...

  10. vue-cli2.0+webpack 项目搭建

    一:准备工作 安装nodejs + 安装webpack + 配置环境变量 => 确保在dos界面的任何路径都都可直接使用命令 二:搭建项目 1.全局安装vue脚手架  [DOS界面] npm i ...