Leetcode 448. Find all Numbers disappeared in an Array
Add to List
Description Submission Solutions
- Total accepted:31266
- Total submissions:58997
- Difficulty:easy
- contributors:yuhaowang001
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]
Subscribe to see which companies asked this question.
Exercises
(1), can not apply for additional space, only in the original array swap operation.
(2), depending on the type of data, the size is in [1, n], the position operation.
Class Solution {public: vector<int> finddisappearednumbers (vector<int>& nums) { vector< Int> ans; int i = 0; while (I<nums.size ()) { if (nums[i]! = nums[Nums[i]-1]) { swap (nums[i], nums[nums[i]-1]); } else{ ++i; } } for (int i=0; i<nums.size (); ++i) { if (nums[i]! = i+1) { ans.push_back (i+1); } } return ans; };
Leetcode 448. Find all Numbers disappeared in an Array