leetcode—217 Contains Duplicate(包含重复的数)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">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,</span>

and it should return false if every element is distinct.

Hide Tags: Array,Hash Table

解题思路:

起初使用的HashMap,一个for循环,将i与数组中的值nums[i],作为键值对进行存储,如果不重复,存入,如果重复直接返回true。 但是在leetcode的OJ编译时,出现了time limited限制。

随后,查阅资料,此处应该使用HashSet,因为HashMap存储时是对键值对进行存储,如果用一个无穷,不重复的数组进行判断,复杂度与时间消耗是很多的。

而HashSet的好处在于:HashSet实现了Set接口,它不允许集合中有重复的值,在进行存储时,先进行判断,使用contain方法即可,复杂度与时间消耗就随之降下来了。

具体可在本人总结的 JAVA学习笔记中:HashMap与HashSet的区别来看,这是一篇转载文献,可供大家参考。

代码如下:

	public static boolean containDuplicate(int []nums)
	{
		Set<Integer> hm=new HashSet<Integer>();
		if (nums.length==0)
		{
			return false;
		}
		int size=nums.length;
		for (int i = 0; i < size; i++)
		{
			if (!hm.contains(nums[i]))
			{
				hm.add(nums[i]);
			}
			else
			{
				return true;
			}
		}
		return false;
	}
时间: 2024-12-20 05:53:03

leetcode—217 Contains Duplicate(包含重复的数)的相关文章

leetCode 217. Contains Duplicate 数组

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. 题目大意: 在数组中找到任意字

leetcode 217. Contains Duplicate 287. Find the Duplicate Number

217. Contains Duplicate 后面3个题都是限制在1-n的 class Solution { public: bool containsDuplicate(vector<int>& nums) { int length = nums.size(); if(length <= 0) return false; sort(nums.begin(),nums.end()); for(int i = 1;i < length;i++){ if(nums[i] ==

LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)

翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容. 如果任何值出现了至少两次,那么返回真(true), 如果每个值都是互不相同的,那么返回假(false). 原文 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 s

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 arr

leetcode 217 Contains Duplicate 数组中是否有重复的数字

 Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

[LeetCode] 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. 这道题不算难题,就是使用一个哈希表,遍历整个数组,如果哈希表里存在,返回fal

leetcode 217 Contains Duplicate 数组中是否有反复的数字

?? Contains Duplicate Total Accepted: 26477 Total Submissions: 73478 My Submissions 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 shoul

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. Subscribe to see which companies asked

Java for 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. 解题思路: HashMap,JAVA实现如下: public boolean