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

 

转载于:https://www.cnblogs.com/jimmycheng/p/7487905.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值