博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]:217:Contains Duplicate
阅读量:7243 次
发布时间:2019-06-29

本文共 709 字,大约阅读时间需要 2 分钟。

题目

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.

 

思路1:直接比较

代码:

public static boolean containsDuplicate(int[] nums) {        int iCounter =0;        for(int i =0;i
1){ return true; } } } return false; }

结果:当测试用例非常大的时候超时

 

思路2:使用Java的集合数据类型HashSet。HashSet中的数据不能重复,add方法返回值为新添加的元素在HashSet集合中是否重复。

代码如下:

public static boolean containsDuplicate1(int[] nums) {        HashSet setAll=new HashSet();        for(int i = 0;i

 

转载于:https://www.cnblogs.com/savageclc26/p/4809611.html

你可能感兴趣的文章
强数学归纳法
查看>>
解析函数论 Page 8 $\log (1+x)$的泰勒展开
查看>>
「陶哲軒實分析」 習題 3.5.5
查看>>
Java基础之 运算符
查看>>
Spring-boot:多模块打包
查看>>
Spring基础13——Spring表达式语言:SpEL
查看>>
MYSQL5.5安装教程
查看>>
C++程序设计(第2版)课后习题答案--第14章
查看>>
网络概念(二)
查看>>
NGUI的Tween动画的使用
查看>>
通过组策略实现IE自动以当前域账号登录某站点
查看>>
实验1成绩汇总
查看>>
数据持久化
查看>>
安装Apache遇到的一点问题
查看>>
Java画出验证码图片
查看>>
第一周的学习进度表
查看>>
Java四种线程池的使用
查看>>
更优雅地关闭资源 - try-with-resource
查看>>
Understanding about numerical stability, convergence and consistency
查看>>
为iOS应用设置图标
查看>>