LeetCode # Array # Easy # 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.

题目:给定一个数组,判断其中有无重复元素,返回true或false。

思路:首先想到用一个hashmap存元素,遍历数组,如果在map中能查到则返回true,否则将该元素存入map。

这种方法空间复杂度较低,时间复杂度为0(N)

 1 import java.util.HashMap;
 2
 3 class Solution {
 4     public boolean containsDuplicate(int[] nums) {
 5         HashMap<Integer, Integer> map = new HashMap<>();
 6         int n = nums.length;
 7         boolean tag = false;
 8         for(int i =0; i< n; i++){
 9             if(map.containsKey(nums[i])){
10                 tag = true;
11             }else{
12                 map.put(nums[i], 1);
13             }
14         }
15         return tag;
16     }
17 }

然后,最佳方法的思路是:先找到最大元素和最小元素,然后设置一个bool类型数组。

如果一个数num-min 的bool变量为空,则是第一次出现,将其bool变量设置成true。若不为空,则说明其为重复元素,返回true。

 1 class Solution {
 2     public boolean containsDuplicate(int[] nums) {
 3         if(nums == null || nums.length == 1) return false;
 4         int max = Integer.MIN_VALUE;
 5         int min = Integer.MAX_VALUE;
 6         for(int num : nums){
 7             if(num > max)
 8                 max = num;
 9             if(num < min)
10                 min = num;
11         }
12         boolean[] bool = new boolean[max - min + 1];
13         for(int num : nums){
14             if(bool[num - min])//如果这个bool值=-true,则返回true
15                 return true;
16             else
17                 bool[num - min] = true;
18         }
19         return false;
20     }
21 }

原文地址:https://www.cnblogs.com/DongPingAn/p/8994655.html

时间: 2024-10-15 13:59:01

LeetCode # Array # Easy # 217. Contains Duplicate的相关文章

Leetcode——array EASY笔记

448. Find All Numbers Disappeared in an Array 误区,O(n)不是要求只能有一个for循环,而是要求,不能有嵌套的for循环!!! 所以可以一个for结束,再一个for结束····这样的 气死了!!数组是 i=0;i<nums.length;i++; 要减一就<=!!!!! //不申请额外空间,即不申请 record数组记录信息 可以采用出现即标记为负数的办法~~hhhh真厉害

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. 大意: 给出一个int型的数组,判断数组是否包含了重复的数.如果有任何

LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

Description Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. F

LeetCode # Array # Easy # 167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

LeetCode # Array # Easy # 665. Non-decreasing Array

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n). 题意:给定一个数组,只调整一个元素的条件下,该数组能否变成

LeetCode # Array # Easy # 697 Degree of an Array

题意:给定一个数组,数组的度是其中出现最多次数的元素.求,最小连续子数组的度和原数组一致. 思路:(参考最佳答案) 遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引.最后一次出现的索引.出现次数.然后,根据counts找到数组的度,再根据right-left求出最小的子数组长度. 1 public class Solution { 2 public int findShortestSubArr

LeetCode--122、167、169、189、217 Array(Easy)

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

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] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill