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.

解法一:

排序,然后比较相邻元素是否相同。时间复杂度O(nlogn),空间复杂度O(1)

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

解法二:

使用unordered_map记录已经出现的元素。时间复杂度O(n),空间复杂度O(n)

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

转载于:https://www.cnblogs.com/ganganloveu/p/4627658.html

【LeetCode】217. Contains Duplicate (2 solutions)相关推荐

  1. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  2. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  3. 【LeetCode】48. Rotate Image (2 solutions)

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  4. 【LeetCode】重复元素相关题目

    [LeetCode]重复元素相关题目 文章目录 [LeetCode]重复元素相关题目 存在重复元素★ 存在重复元素 II★ 存在重复元素 III★★ 重复 N 次的元素★ 寻找重复数★★ 存在重复元素 ...

  5. 【LeetCode】#39组合总和(Combination Sum)

    [LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...

  6. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  7. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  8. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  9. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  10. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

最新文章

  1. Python基础06-数据类型:元组tuple
  2. 使用python手写FFT算法
  3. 实验管理系统java,大学生创新实验室信息管理系统 java+mysql
  4. 一键安装 redmine on windows 和发邮件设置
  5. 数据集标签_数据分享 | LiDAR点云数据汇总
  6. Hexo及Next主题配置(最新版)
  7. java程序加密_对Java代码加密的两种方式,防止反编译
  8. 一文介绍解银行卡验证api接口详情
  9. 拼多多被“薅羊毛” 交千万学费学到了什么?
  10. 高数——微分中值定理之拉格朗日与柯西
  11. Mac 平台下功能强大的Shimo软件使用指南
  12. oracle 百分比换算问题
  13. 装了xmapp还需要装mysql吗_安装xamp之后,appach、mysql等问题的总结
  14. 电脑唯独搜不到自己家wifi,怎么办?
  15. kb mac压缩图片大小_Mac系统怎么压缩图片
  16. 终于弄懂 CRC 循环冗余校验 辽
  17. axios的响应拦截器
  18. css内联样式_如何覆盖内联CSS样式
  19. 从开发转到安全渗透工程师,是我做的最对的决定
  20. zoom会议设置,zoom 如何设置会议密码,每一步骤都要看一下。

热门文章

  1. 【NLP】45个小众而实用的NLP开源字典和工具
  2. 【每日算法Day 105】打家劫舍第二弹:看好你的电瓶车!
  3. 每日算法系列【LeetCode 289】生命游戏
  4. [TACL17]基于中序转移的成分句法分析
  5. 具体数学-第1课(递归求解实际问题)
  6. 03 ZooKeeper底层原理剖析与命令实战
  7. Datawhale编程学习之算法思想(7)
  8. 深度学习花书- 4.3 基于梯度的优化方法
  9. Django ORM models操作
  10. C语言中的指针型函数值