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.

题目大意:

给定一个整数数组,判断数组中是否包含重复元素。如果数组中任意一个数字出现了至少两次,你的函数应该返回true,如果每一个元素都是唯一的,返回false。

解题方法:

先对数组进行排序,比较相邻俩元素是否相等。

注意事项:

C++代码:

4~18行为谢尔排序,可用sort(nums.begin(),nums.end())代替。

  1. class Solution {
  2. public:
  3. bool containsDuplicate(vector<int>& nums) {
  4. int tmp;
  5. int j;
  6. if(nums.empty()||nums.size()==) return false;
  7. for(int gap=nums.size()/;gap>;gap/=)
  8. {
  9. for(int i=gap;i<nums.size();i++)
  10. {
  11. tmp=nums[i];
  12. for(j=i;j>=gap&&tmp<nums[j-gap];j-=gap)
  13. {
  14. nums[j]=nums[j-gap];
  15. }
  16. nums[j]=tmp;
  17. }
  18. }
  19. for(int i=;i<nums.size()-;i++)
  20. {
  21. if(nums[i]==nums[i+]) return true;
  22. }
  23. return false;
  24. }
  25. };

217. Contains Duplicate(C++)的更多相关文章

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

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

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

  3. 机器学习与Tensorflow(2)——神经网络及Tensorflow实现

    神经网络算法以及Tensorflow的实现 一.多层向前神经网络(Multilayer Feed-Forward Neural Network) 多层向前神经网络由三部分组成:输入层(input la ...

  4. (转载)[MySQL技巧]INSERT INTO… ON DUPLICATE KEY UPDATE

    (转载)http://blog.zol.com.cn/2299/article_2298921.html MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDA ...

  5. (转载)INSERT INTO .. ON DUPLICATE KEY 语法与实例教程

    (转载)http://www.111cn.net/database/mysql/ON_DUPLICATE_KEY%20.htm INSERT语句末尾指定了ON DUPLICATE KEY UPDATE ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  8. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. Android(java)学习笔记217:开发一个多界面的应用程序之清单文件

    清单文件的重要参数:     <intent-filter>             代表的应用程序的入口界面           <action android:name=&quo ...

随机推荐

  1. glsl-UBO

    UBO 是什么?为何要用UBO? 1.数据共享设计 采用Block的原因是: 如果你的程序中包含了多个着色器,而且这些着色器使用了相同的Uniform变量,你就不得不为每个着色器分别管理这些变量.Un ...

  2. Linux Mono Asp.net 部署方案

    1.Jexus 国内的 官网:http://www.jexus.org 2.Apache 官网:http://mono-project.com/Mod_mono 3.Nginx 官网:http://m ...

  3. 使用DNSAgent拦截特定域名

    开发程序时,为方便测试,需要把本来发往abc.com的数据发到本地. 最简单的方法是直接在程序中修改,把abc.com修改为需要的地址. 但这样提交代码时,容易把调试地址给提交到服务器. 或是嵌入式设 ...

  4. MAC 下安装PIL

    1. 安装使用 pip install pil 结果报如下错误 Collecting PIL Could not find a version that satisfies the requireme ...

  5. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  6. Android——Cocosd2d-x手机游戏开发学习思路

    手机APP应用如雨后春笋般冒了出来,而在众多的APP应用中,游戏占据了半壁江山.它丰富着人们的业余生活,增进了人们之间的沟通交流.也有许多开发的朋友对游戏开发情有独钟,他们不止是享受着有很多的人们去下 ...

  7. Spring Aop重要概念介绍及应用实例结合分析

    转自:http://bbs.csdn.net/topics/390811099 此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细 ...

  8. MINA学习之体系介绍

    基于MINA应用程序结构图: 我们可以看出,MINA是应用程序(客户端或服务端)和底层基于TCP,UDP等通讯协议的网络层之间的粘合剂.而且各个模块之间是相互独立的,你只需要在MINA体 系基础上设计 ...

  9. 用Android++在Visual Studio中用C/C++开发Android应用

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用Android++在Visual Studio中用C/C++开发Android应用.

  10. WEB DYNPRO SAP HELP

    http://help.sap.com/saphelp_nw70ehp1/helpdata/EN/43/70d83e8cb23d67e10000000a114084/content.htm