leetcode - find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:Input:  [4,3,2,7,8,2,3,1]

Output: [2,3]

这个题目我也是看了答案才想得到是这个做法,然后过几天再做一次ac了。

首先看下题目,题目提示a[i]<= n。这个限制肯定是有目的的,我们可以推测我们在大案例应该会用到a[i]的值用作index

这个题目的思路如下:

因为我们要找到重复的数字,所以我们需要找到一个方法标记一下当前数字已经出现过了,再加上题目提示每一个数字都是小于array size,所以我们就接着推测用当前数字 - 1作为index (因为数字有可能是等于数组大小),然后再把这个index上的数字改为负数: [1,2,3,4,5] -> 当前数字为1, 那么 index(1-1 = 0) 上的数字, which is 1 就要变成-1. 

因为下一次再到第二次出现1的时候, 我们会找回到这个 1-1=0 的index 上,看是否已经出现过一次。

为了保证遍历的时候数字不是负数,我们用abs(numbers[i])。

 1 class Solution {
 2     public List<Integer> findDuplicates(int[] nums) {
 3         List<Integer> ls = new ArrayList<>();
 4         for (int i = 0; i < nums.length; i++) {
 5             int current = Math.abs(nums[i]);
 6             int match  = nums[current - 1];         // if the number on the index is negative, that means the number has appeared once
 7             if (match < 0) {
 8                 ls.add(current);
 9             } else {
10                 nums[current - 1] = -1 * match;
11             }
12         }
13         return ls;
14     }
15 }
 
时间: 2024-10-10 04:48:00

leetcode - find All Duplicates in an Array的相关文章

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

[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

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

[LeetCode][JavaScript]Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of n

leetCode 26. Remove Duplicates from Sorted Array 数组

26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.

【leetcode】Remove Duplicates from sorted array

Remove Duplicates from sorted array 问题描述 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant me

LeetCode OJ Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't