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. git/gitLab

    gitlab安装:http://www.360doc.com/content/15/0603/14/21631240_475362133.shtml http://www.cnblogs.com/wi ...

  2. Hadoop! | 大数据百科 | 数据观 | 中国大数据产业观察_大数据门户

        你正在使用过时的浏览器,Amaze UI 暂不支持. 请 升级浏览器 以获得更好的体验! 深度好文丨读完此文,就知道Hadoop了! 来源:BiThink 时间:2016-04-12 15:1 ...

  3. URL、Session、Cookies、Server.Transfer、Application和跨页面传送,利弊比较

    URL.Session.Cookies.Server.Transfer.Application和跨页面传送.-本题考查面试者对ASP.NET中多页面传值的理解是否全面.因为ASP.NET的页面表单提交 ...

  4. mysql索引优化

    mysql 索引优化 >mysql一次查询只能使用一个索引.如果要对多个字段使用索引,建立复合索引. >越小的数据类型通常更好:越小的数据类型通常在磁盘.内存和CPU缓存中都需要更少的空间 ...

  5. ubutu之jdk安装

    1.jdk下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.解压jdk- ...

  6. Nginx初学者指南

    Starting, Stopping, and Reloading Configuration To start nginx, run the executable file. Once nginx ...

  7. BZOJ3172——[Tjoi2013]单词

    1. 题目大意:一篇论文是由许多单词组成,现在想知道每个单词分别在论文中出现多少次. 2.分析:对着 广义后缀自动机的图看,我们就会发现玄机,答案不就是这个单词下的后缀个数吗? 于是建立自动机,然后求 ...

  8. HTML5的新特性及技巧分享总结

    原文链接:http://www.aseoe.com/show-10-645-1.html?utm_source=tuicool&utm_medium=referral 1. 新的Doctype ...

  9. word20161203

    B-channel / B 信道 B-ISDN, broadband integrated services digital network / 广播综合业务数字网络 backbone router  ...

  10. Fibonacci 1

    Fibonacci 1 题面 \[F_0=0,F_1=1,F_n=F_{n-1}+F_{n-2}\] 给定\(n\),求 \[S(n)=\sum_{i=1}^{n}F_nF_{n-1}\] 数据格式 ...