Description
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?
1 | Input: |
Method
要在O(n)时间复杂度之内完成的话,就只能遍历一次数组。扫一遍数组然后将扫描过的数放进HashSet
里,然后判断数有没有在HashSet
中即可。
Solution
1 | public class Solution { |