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.

题目:给定一个数组,判断其中有无重复元素,返回true或false。

思路:首先想到用一个hashmap存元素,遍历数组,如果在map中能查到则返回true,否则将该元素存入map。

这种方法空间复杂度较低,时间复杂度为0(N)

 1 import java.util.HashMap;
 2
 3 class Solution {
 4     public boolean containsDuplicate(int[] nums) {
 5         HashMap<Integer, Integer> map = new HashMap<>();
 6         int n = nums.length;
 7         boolean tag = false;
 8         for(int i =0; i< n; i++){
 9             if(map.containsKey(nums[i])){
10                 tag = true;
11             }else{
12                 map.put(nums[i], 1);
13             }
14         }
15         return tag;
16     }
17 }

然后,最佳方法的思路是:先找到最大元素和最小元素,然后设置一个bool类型数组。

如果一个数num-min 的bool变量为空,则是第一次出现,将其bool变量设置成true。若不为空,则说明其为重复元素,返回true。

 1 class Solution {
 2     public boolean containsDuplicate(int[] nums) {
 3         if(nums == null || nums.length == 1) return false;
 4         int max = Integer.MIN_VALUE;
 5         int min = Integer.MAX_VALUE;
 6         for(int num : nums){
 7             if(num > max)
 8                 max = num;
 9             if(num < min)
10                 min = num;
11         }
12         boolean[] bool = new boolean[max - min + 1];
13         for(int num : nums){
14             if(bool[num - min])//如果这个bool值=-true,则返回true
15                 return true;
16             else
17                 bool[num - min] = true;
18         }
19         return false;
20     }
21 }

转载于:https://www.cnblogs.com/DongPingAn/p/8994655.html

LeetCode # Array # Easy # 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 (Tree easy)

    leetcode Tree easy # leetcode Tree easy problem class TreeNode(object):def __init__(self,val):self.v ...

  4. leetcode python3 简单题217. Contains Duplicate

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百一十七题 (1)题目 英文: Given an array of intege ...

  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】

    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. [LeetCode]Array主题系列{35,39,40,48题}

    1. 内容介绍 开一篇文章记录在leetcode中array主题下面的题目和自己的思考以及优化过程,具体内容层次按照{题目,分析,初解,初解结果,优化解,优化解结果,反思}的格式来记录,供日后复习和反 ...

最新文章

  1. 详解Jedis连接池报错处理
  2. Mysql学习总结(4)——MySql基础知识、存储引擎与常用数据类型
  3. 除了航拍,无人机也许能用于救火救灾
  4. Android微信视频播放填坑指南
  5. 我对创业和管理的一些看法
  6. java借口案例实现_java实现接口的典型案例
  7. 数据结构树4-二叉搜索树2
  8. Linux下的“句柄”(文件句柄,窗口句柄)
  9. 5去掉button按钮的点击样式_各种好看的小按钮合集,纯css编写,最近在学习时遇到的,记录成为笔记...
  10. mysql 模糊查询之特殊字符下划线 _
  11. HM16.0之帧间Merge模式——xCheckRDCostMerge2Nx2N
  12. 全局钩子,解决命名烦恼!——代码翻译小工具。
  13. Unity自动设置keystore密匙库的信息
  14. 【头发渲染】Technical Artist的不归路 —— Kajiya-Kay Shading
  15. %几.几//C语言(闲的没事,记录下)
  16. misc类设备驱动1——板载蜂鸣器驱动测试
  17. putty永久设置session
  18. 一文带你了解Room数据库
  19. 大学生活质量指北,高考毕业生填报志愿参考必备
  20. Error:Module ‘javase‘ production: java.lang.ClassCastException:

热门文章

  1. marc数据个人心得
  2. Android安卓模拟器的使用
  3. springboot的redis工具类编写(采用RedisTemplate)(简单的取值,取多个值)。
  4. pixhawk篇之坐标系转化,相关转化矩阵知识,算法截取
  5. 流水线、超流水线、超标量技术对比
  6. 设计模式C++实现——观察者模式
  7. 关于map的下标操作的2个例子
  8. Leetcode题库203.移除链表元素(尾指针填充 / 虚头指针定义 c实现)
  9. 计算机组成原理-数制与编码
  10. [BUUCTF-pwn]——jarvisoj_level302-21