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

时间: 2024-12-10 20:21:03

LeetCode 217. Contains Duplicate (包含重复项)的相关文章

LeetCode 219. Contains Duplicate II (包含重复项之二)

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 题目标签:Array, Hash Table 题目给了我们一个nums array 和一个 k, 让

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(包含重复的数)

<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 t

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