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 The does not appear in this array.
Could do it without extra space and in O (n) runtime? Assume the returned list does not count as extra space.
Example:
input:[4,3,2,7,8,2,3,1]output:[5,6]
Solution:
Mark the value of already apeared index to negative and then loop it through to find the positive ones and then the index of PO Sitive ones is the result;
1 Public classSolution {2 Publicilist<int> Finddisappearednumbers (int[] nums) {3 intn =Nums. Length;4ilist<int> result =Newlist<int>();5 for(intI=0; i<n;i++)6 {7 intindex =Math.Abs (Nums[i]);8 if(nums[index-1]>0)9 {Tennums[index-1]=-nums[index-1]; One } A } - for(intI=0; i<n;i++) - { the if(nums[i]>0) - { -Result. ADD (i+1); - } + } - returnresult; + } A}
LeetCode-448. Find all Numbers disappeared in an Array C #