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?

思路:还是一个映射,如果某个位置要映射的时候已经为负了,就说明之前映射过该位置,那么该位置对应的元素就是出现了两个的元素。

和leetcode 448是一对题目。

 

作者: CrazyKK

ex-ACMer@hust,stackoverflow-engineer@sensetime

说点什么

您将是第一位评论人!

提醒
wpDiscuz