Problem:

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.

Summary:

判断数组中是否出现重复值。

Analysis:

1. 给数组排序后遍历,判断相邻两值是否相等。

  1. class Solution {
  2. public:
  3. bool containsDuplicate(vector<int>& nums) {
  4. int len = nums.size();
  5.  
  6. sort(nums.begin(), nums.end());
  7. for (int i = ; i < len; i++) {
  8. if (nums[i] == nums[i - ]) {
  9. return true;
  10. }
  11. }
  12.  
  13. return false;
  14. }
  15. };

2. Hash表建立数组中数字和出现次数的映射,判断是否大于1。

  1. class Solution {
  2. public:
  3. bool containsDuplicate(vector<int>& nums) {
  4. int len = nums.size();
  5. unordered_map<int, int> m;
  6.  
  7. for (int i = ; i < len; i++) {
  8. if(++m[nums[i]] > ) {
  9. return true;
  10. }
  11. }
  12.  
  13. return false;
  14. }
  15. };

LeetCode 217 Contains Duplicate的更多相关文章

  1. 25. leetcode 217. Contains Duplicate

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

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

  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 217. Contains Duplicate (包含重复项)

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

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

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

  7. leetcode 217 Contains Duplicate 数组中是否有反复的数字

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

  8. Java for LeetCode 217 Contains Duplicate

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

  9. (easy)LeetCode 217.Contains Duplicate

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

随机推荐

  1. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...

  2. linq学习

    最全的linq学习文章: http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html

  3. [译]我是怎么构建Node.js程序的

    原文: http://blog.ragingflame.co.za/2015/4/1/how-i-build-nodejs-applications "保持简单, 保持模块化." ...

  4. 【codevs1163】访问艺术馆

    题目描述 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室.皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的 ...

  5. Hadoop 之Mong DB 之CentOS 6 使用 yum 安装MongoDB及服务器端配置

    安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法.由于MongoDB更新得比较快,我比较喜欢用yum源安装的方法.64位Centos下的安装步骤如下: 1.准 ...

  6. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法

    1.错误:  Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  7. ubuntu 15.04 手动安装nginx 1.9.0

    平时工作也用nginx,不过用的时候都是已经配好的,只要简单改改参数就可以了.今天在自己的电脑上安装的时候发现没有想象的那么顺利. 纸上得来终觉浅,绝知此事要躬行. 正题: 1.到nginx下载页面获 ...

  8. Android 利用SharedPreferences保存与删除 用户登录数据

    创建SharedPreferences对象: SharedPreferences sharedPreferences = context.getSharedPreferences("user ...

  9. Java多线程文件下载

    一. 多线程下载文件考虑处理步骤: 1. 如何获取文件的长度 2. 合理的创建线程数量,并计算每一个线程下载的长度 3. 如何将多个线程下载的字节写入到文件中 二. 代码实现如下: package c ...

  10. HNU 12869 Sequence(循环节)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12869 解题报告:看到n的范围这么大,一看就是找规律,把前30 ...