标签:example runtime auto back lan appear this round return
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]
public class Solution {
public IList<int> FindDuplicates(int[] nums) {
List<int> list = new List<int>();
for (int i = 0; i < nums.Length; i++) {
nums[Math.Abs(nums[i]) - 1] *= -1;
if (nums[Math.Abs(nums[i]) - 1] > 0) {
list.Add(Math.Abs(nums[i]));
}
}
return list;
}
}
442. 找出数组中重复的元素 Find All Duplicates in an ArrayGiven an array of integers
标签:example runtime auto back lan appear this round return
原文地址:http://www.cnblogs.com/xiejunzhao/p/fd62258e250caf4f07840c19bb9fac0d.html