# C#LeetCode刷题之#448-找到所有数组中消失的数字（Find All Numbers Disappeared in an Array）

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Input:[4,3,2,7,8,2,3,1]

Output:[5,6]

```public class Program {

public static void Main(string[] args) {
int[] nums = null;

nums = new int[] { 4, 3, 2, 7, 8, 2, 3, 1 };
var res = FindDisappearedNumbers(nums);
ShowArray(res);

res = FindDisappearedNumbers2(nums);
ShowArray(res);

}

private static void ShowArray(IList<int> array) {
foreach(var num in array) {
Console.Write(\$"{num} ");
}
Console.WriteLine();
}

private static IList<int> FindDisappearedNumbers(int[] nums) {
//此解法超时，LeetCode没有AC
int max = nums.Length;
var res = new List<int>();
if(max == 0) return res;
for(int i = 1; i <= max; i++) {
if(!nums.Contains(i)) {
}
}
return res;
}

private static IList<int> FindDisappearedNumbers2(int[] nums) {
var res = new List<int>();
for(int i = 0; i < nums.Length; i++) {
//记录索引，位置为i处的值的绝对值减1
//（因为数值范围从1..n所以要减1）作为临时索引位
int index = Math.Abs(nums[i]) - 1;
//如果此处的值大于0，那么将其变为负数，
//没有变为负数的值即为不存在于数组中的数字，
//直接变为负数不至于使其值丢失，只需要取绝对值即可恢复原值
if(nums[index] > 0) {
nums[index] = -nums[index];
}
}
//所有值大于0的即为“迷失的数字”
for(int i = 0; i < nums.Length; i++) {
if(nums[i] > 0) {
}
}
return res;
}

}```

```5,6
5,6```

FindDisappearedNumbers的解法由于使用了nums.Contains(i)检查是否存在某个值，若其时间复杂度为线性的，则FindDisappearedNumbers的时间复杂度应为：  ，FindDisappearedNumbers2的解法时间复杂度为：  。