LeetCode 217. 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.


题目标签:Array, Hash Table

  题目给了我们一个nums array, 让我们来检查它是否有重复的数字。遍历nums array, 如果hash set 没有这个数字,就存入;有的话,直接返回true。

 

Java Solution:

Runtime beats 50.21% 

完成日期:05/26/2017

关键词:Array, Hash Table

关键点:利用Hash set 的特性,独一性

 

复制代码
 1 public class Solution 
 2 {
 3     public boolean containsDuplicate(int[] nums) 
 4     {
 5         if(nums.length == 0 || nums == null)
 6             return false;
 7         
 8         HashSet<Integer> unique = new HashSet<>();
 9         
10         for(int i=0; i<nums.length; i++)
11         {
12             if(unique.contains(nums[i]))
13                 return true;
14             else
15                 unique.add(nums[i]);
16         }
17         
18         
19         return false;
20     }
21 }
复制代码

参考资料:N/A

 

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

 

posted @   Jimmy_Cheng  阅读(156)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示