442. Find All Duplicates in an Array(LeetCode)

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]
 1 class Solution {
 2 public:
 3     vector<int> findDuplicates(vector<int>& nums) {
 4         vector<int> res;
 5         for (int i = 0; i < nums.size(); i++){
 6             nums[abs(nums[i]) - 1] = -nums[abs(nums[i]) - 1];
 7             if (nums[abs(nums[i]) - 1] > 0) res.push_back(abs(nums[i]));
 8         }
 9         return res;
10     }
11 };
时间: 2024-10-08 06:46:54

442. Find All Duplicates in an Array(LeetCode)的相关文章

leetcode_26题——Remove Duplicates from Sorted Array (string)

Remove Duplicates from Sorted Array Total Accepted: 57887 Total Submissions: 183534My Submissions Question Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocat

LeetCode 442. 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,

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. For example,Given input array A = [

leetcode 之Remove Duplicates from Sorted Array(二)

描述    Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?    For example, Given sorted array A = [1,1,1,2,2,3],    Your function should return length = 5, and A is now [1,1,2,2,3] 之前的想法是再加一个计数的变量就行了 int removeDeplicates1(

leetcode 之Remove Duplicates from Sorted Array(一)

删除数组中的重复元素并返回新数组的个数 思路:保留不同的元素即可. 1 int removeDeplicates(int A[], int n) 2 { 3 int index = 0; 4 for (int i = 0; i < n; i++) 5 { 6 if (A[index] != A[i]) 7 { 8 A[index++] = A[i]; 9 } 10 } 11 12 return index + 1; 13 }

LeetCode:26. Remove Duplicates from Sorted Array(Easy)

1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度 注意:不能重新创建一个数组,空间复杂度为O(1) 3. 解题思路 使用指针j来遍历数组,i用来计数.初始时,i指向nums[0],j指向nums[1]. 当nums[i] != nums[j]时,i++,且nums[i]=nums[j],

540. Single Element in a Sorted Array(LeetCode)

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

442. Find All Duplicates in an Array

   题目描述     442. Find All Duplicates in an Array   题目分析 遍历数组,将每次取出的值放入到正确的位置上,如nums[0]的值为4,则将4放入到nums[3]中(交换值方式). 如果正确的位置已经有正确的值(nums[index]==index+1),则不用处理. 如此,将不断地将值放入到正确的位置.而放不进去的值则为重复的值. 补充:可以使用LeetCode 448的思路,遍历数组,每取一值,index=nums[index]走一遍并一路做好标

【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】

[026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 anot