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 ≤a[i]≤n(n= size ofarray) ?如果按坐標點來追蹤的話, 如果一個數字出現兩次 那么這個坐標點必定訪問兩次, ?nums[nums[i]-1] 設置為負值。 如果發現為負 那么這個值就說明已經出現過一次。