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. 给数组排序后遍历,判断相邻两值是否相等。

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

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

 class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len = nums.size();
unordered_map<int, int> m; for (int i = ; i < len; i++) {
if(++m[nums[i]] > ) {
return true;
}
} return false;
}
};

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. Reflect(欧拉函数)

    Reflect Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. AChartEngine 安卓折线图 柱形图等利器

    http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多de ...

  3. HDU 4803 Poor Warehouse Keeper

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4803 解题报告:有一个记录器,一共有两个按钮,还有两行屏幕显示,第一行的屏幕显示的是数目,第二行的屏幕 ...

  4. BZOJ2843——极地旅行社

    1.题目大意:动态树问题,点修改,链查询.另外说明双倍经验题=bzoj1180 2.分析:lct模板题,练手的 #include <stack> #include <cstdio&g ...

  5. Linux 利器- Python 脚本编程入门(一)

    导读 众所周知,系统管理员需要精通一门脚本语言,而且招聘机构列出的职位需求上也会这么写.大多数人会认为 Bash (或者其他的 shell 语言)用起来很方便,但一些强大的语言(比如 Python)会 ...

  6. [KOJ6024]合并果子·改(强化版)

    [COJ6024]合并果子·改(强化版) 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多把这些果子堆排成一排,然后所有的果子合成一堆.    每一次合并 ...

  7. iOS中的时间和日期

    怎么说?时间和日期不是了不起的属性.了不起的功能,但是,我们决不能够因此就“冷落”它. 一:怎么“搞到货”--如何获取时间.日期 //-=-==当前时间------默认显示“0时区”时间 NSDate ...

  8. spring 部分配置内容备忘

    1.spring定时器简单配置: <bean name="taskJob" class="com.netcloud.mail.util.TaskJob"& ...

  9. mysql互换表中两列数据方法

    1.创建表及记录用于测试 ) unsigned ) ,) unsigned ,) unsigned NOT NULL COMMENT '现价', PRIMARY KEY (`id`) ) ENGINE ...

  10. Java Log4j日志使用

    在程序中使用log4j 1.导入包import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator; 2.获取lo ...