找重复元素,两种方法

第一种:排序,有重复元素,必然出现在相邻位置

 1 int x=[](){
 2     std::ios::sync_with_stdio(false);
 3     cin.tie(NULL);
 4     return 0;
 5 }();
 6
 7 class Solution
 8 {
 9 public:
10     bool containsDuplicate(vector<int>& nums)
11     {
12         int size = nums.size();
13         if(size <= 1) return false;
14         sort(nums.begin(), nums.end());
15         for(int i = 0; i < size-1; ++i)
16         {
17             if(nums[i] == nums[i+1]) return true;
18         }
19         return false;
20     }
21
22 };

第二种:利用关联容器计数,计数值大于1,必然有重复元素。

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7
 8 class Solution
 9 {
10 public:
11     bool containsDuplicate(vector<int>& nums)
12     {
13         unordered_map<int,int> vmap;
14         for(int i : nums)
15         {
16             vmap[i]++;
17             if(vmap[i]>1)
18                 return true;
19         }
20         return false;
21     }
22 };

转载于:https://www.cnblogs.com/zhuangbijingdeboke/p/9077157.html

217. Contains Duplicate相关推荐

  1. LeetCode刷题记录2——217. Contains Duplicate(easy)

    LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...

  2. 217. Contains Duplicate - LeetCode

    为什么80%的码农都做不了架构师?>>>    Question 217. Contains Duplicate Solution 题目大意:判断数组中是否有重复元素 思路:构造一个 ...

  3. LeetCode 217. Contains Duplicate

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

  4. LeetCode【217. Contains Duplicate】

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

  5. 217. Contains Duplicate数组重复元素 123

    [抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...

  6. LeetCode # Array # Easy # 217. Contains Duplicate

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

  7. Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]

    题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...

  8. [Array]217.Contains Duplicate

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

  9. 217 Contains Duplicate

    public class Solution {public boolean containsDuplicate(int[] nums) {HashSet<Integer> hs = new ...

最新文章

  1. Python之机器学习K-means算法实现
  2. Python使用matplotlib可视化散点图、可视化两个数值变量之间的关系(Scatter plot)
  3. 6个让比特币现金保持匿名性的最佳方法
  4. Altium Designer快捷键失效恢复方法
  5. git 撤销刚才的rebase_git rebase 的使用 (用于撤销某次commit)
  6. docker 在esx上的网络配置
  7. 【Flask】request请求
  8. request,response,session,application,out对象的常用调用的函数
  9. spring的aop整理
  10. C++笔记-利用远程线程注入获取PC版微信个人昵称
  11. Python菜鸟入门:day13编程学习
  12. CentOS 6.3 配置 yum
  13. (转)AssetBundle系列——游戏资源打包(二)
  14. HTML流式布局是什么,什么是流式布局?流式布局是什么意思?
  15. 民航产业发展趋势及对策建议(2021-10-21)
  16. python27.dll引起的appcrash_Python已经停止工作(APPCRASH)Python
  17. latex大括号 多行公式_如何快速入门 LaTeX,在 XMind 2020 中轻松输入方程。
  18. 机器学习mAP之我见
  19. 学生动漫网页设计模板下载 海贼王大学生HTML网页制作作品 简单漫画网页设计成品 dreamweaver学生网站模板
  20. 爬取糗事百科段子(xpath)

热门文章

  1. jersey球衣是什么_球衣知识------关于NIKE球衣(Jersey)的介绍
  2. Oracle就业课第六课之游标和触发器
  3. JAVAEE框架之Spring AOP
  4. 记一次偶遇Adminer
  5. Python爬虫神器pyppeteer
  6. 【Linux】/etc/sysconfig/i18n文件详解
  7. xshell可以连接上linux,但是xftp却连不上问题
  8. 2016校招腾讯研发岗笔试题---递归法求解格雷码
  9. Python使用os.listdir()函数来得目录内容的介绍
  10. ubuntu14.0.4下安装pycharm