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.

分析:

HashSet?

first try:

import java.util.HashSet;class Solution {public boolean containsDuplicate(int[] nums) {HashSet<Integer> hashSet = new HashSet<Integer>();for(int i=0; i<nums.length; i++) {if(hashSet.contains(nums[i]) ) {return true;} else {hashSet.add(nums[i]);}}return false;}
}

result:

second:

import java.util.HashSet;class Solution {public boolean containsDuplicate(int[] nums) {HashSet<Integer> hashSet = new HashSet<Integer>();for(int i=0; i<nums.length; i++) {int num = nums[i];if(hashSet.contains(num) ) {return true;} else {hashSet.add(num);}}return false;}
}

result:

the same.

third:

import java.util.HashSet;class Solution {public boolean containsDuplicate(int[] nums) {HashSet<Integer> hashSet = new HashSet<Integer>();int length = nums.length;for(int i=0; i<length; i++) {int num = nums[i];if(hashSet.contains(num) ) {return true;} else {hashSet.add(num);}}return false;}
}

result:

anylysis:

看了solution里的代码,并执行了一下,发现比我的代码还慢。

import java.util.HashSet;public class Solution {public boolean containsDuplicate(int[] nums) {Set<Integer> set = new HashSet<Integer>();for (int x: nums) {if (set.contains(x)) {return true;   } else {set.add(x);}}return false;}
}

result:

32.96%

import java.util.HashSet;public class Solution {public boolean containsDuplicate(int[] nums) {Set<Integer> set = new HashSet<Integer>();int length = nums.length;for(int i=0; i<length; i++) {if (set.contains(nums[i])) {return true;   } else {set.add(nums[i]);}}return false;}
}

17.88%

发现慢的原因在于Set,如果是HashSet速度会变快。

for 和foreach循环速度在都是HashSet时没有变化;在是Set时有变化。

发现这种优化费时费力,放弃。

总结:

import java.util.HashSet;

public class Solution {
    public boolean containsDuplicate(int[] nums) {
    Set<Integer> set = new HashSet<Integer>();
    for (int x: nums) {
        if (set.contains(x)) {
            return true;   
        } else {
            set.add(x);
        }
       
    }
    return false;
    }
}

转载于:https://www.cnblogs.com/hzg1981/p/8875514.html

LeetCode 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 python3 简单题217. Contains Duplicate

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

  4. LeetCode【217. Contains Duplicate】

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

  5. LeetCode # Array # Easy # 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--python,java解法--set--简单

    题目地址:Contains Duplicate - LeetCode 题目: Given an array of integers, find if the array contains any du ...

  7. LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  8. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  9. LeetCode 217、219. 存在重复元素

    217. 存在重复元素 题目:给你一个整数数组 nums .如果任一值在数组中出现 至少两次 ,返回 true :如果数组中每个元素互不相同,返回 false . 链接 https://leetcod ...

最新文章

  1. 【小虫虫】邮购笔记本的注意事项
  2. 轻松弄懂var、let、const之间的区别(一看就懂)
  3. (33)System Verilog类1调用类2示例
  4. Prompt范式,真香
  5. 统计一个字符串中英文字母、空格、数字和其它字符的个数
  6. Hive与RDBMS的区别
  7. python爬虫——爬取b站APP视频信息(通过fiddler抓包工具)
  8. python爬虫json数据解析_爬虫的解析方式一:JOSN解析
  9. 北京地区的汉代古驿道粗探
  10. “智慧银行”行业研究报告要点整理
  11. Jenkins项目配置-Nodejs项目(Vue框架)-全面
  12. 【吐血整理】一份完备的集成学习手册!(附Python代码)
  13. 手把手带你撸一个校园APP(七):校园文化模块(社团活动表白墙图说校园)
  14. 用Python写一个企业微信自动化打卡的脚本
  15. 传输层协议(TPC和UDP介绍)
  16. Apache IoTDB 鼠年总结
  17. Python JPG文件或DCM文件Mask掩膜ROI手动圈画轮廓
  18. iframe 自适应高度的多种实现方式
  19. 安卓手机后台Service自动转发短信
  20. 如何将电子海图的航线导到gps上_基于GPS和电子海图的船舶导航系统设计与实现...

热门文章

  1. 使用microsoft EnterpriseLibrary连接不同数据库简单应用 .
  2. Hadoop之mapreduce 实例三
  3. 探寻背后的机制化繁为简:网站程序升级不过是文件同步
  4. 配置EXCHANGE服务器
  5. Loadrunner错误-26601、-27492、-27727处理方法
  6. 《Git in Practice》作者访谈:关于Git的八个问题
  7. mac os 快捷键
  8. Professional WCF 4读书笔记(2)——消息交换模式
  9. CodeChef TWOROADS(计算几何+拉格朗日乘数法)
  10. 2.1/2.2 系统目录结构, 2.3 ls命令, 2.4 文件类型, 2.5 alias命令