Leetcode 448 Find All Numbers disappeared in an Array

Source: Internet
Author: User

Problem:

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]

Analysis:

All the numbers in the shaped array A of length n are greater than or equal to 1, less than or equal to N, which may contain numbers that repeat two times.

Output [1, n] does not exist in the collection of numbers in array a.

Summary:

1. The first thought is to create a hash table with a map, the key is the number of occurrences in the array, value is the number of occurrences, and then the number that is found 0 times.

1 classSolution {2  Public:3vector<int> Finddisappearednumbers (vector<int>&nums) {4         intLen = Nums.size (), Maxnum =-1;5 sort (Nums.begin (), Nums.end ());6map<int,int>m;7vector<int>Res;8         9          for(inti =0; i < Len; i++) {Tenm[nums[i]]++; OneMaxnum = Maxnum < Nums[i]?Nums[i]: maxnum; A         } -          -          for(inti =1; I <= Len; i++) { the             if(!M[i]) { - Res.push_back (i); -             } -         } +          -         returnRes; +     } A};

However, this method is not feasible because the array tag is not required in the topic.

2. Without consuming the rest of the space, the first thought is to sort the array, using a variable K to record the next number in [1, n] to be found, and to save the numbers that do not exist in the array.

Attention needs to be handled when duplicate numbers occur.

1 classSolution {2  Public:3vector<int> Finddisappearednumbers (vector<int>&nums) {4         intLen =nums.size ();5 sort (Nums.begin (), Nums.end ());6vector<int>Res;7         8         intK =1;9          for(inti =0; i < Len; i++) {Ten             if(k! =Nums[i]) { One                  while(K <Nums[i]) { ARes.push_back (k++); -                 } -             } the              -             if(I < Len-1&& Nums[i] = = Nums[i +1]) { -i++; -             } +              -k++; +         } A          at          while(k <=Len) { -Res.push_back (k++); -         } -          -         returnRes; -     } in};

But because of the sort, the time complexity is O (NLOGN), does not satisfy the question O (n), therefore this method still is not OK.

3. Because all of the numbers in the array are greater than or equal to 1 and less than or equal to N, and N is the length of this condition. So you can mark which numbers appear as an array subscript as an index, and mark the number as negative if the number corresponding to the subscript appears.

    1. Calculates the subscript form of the number x appearing in the array, the following table is index = |x| -1, you can mark the array a a[index] as a negative number to flag the x appears in the array
    2. If a number in the array is negative, it indicates that the number appears in the array
    3. The final traversal of the array, if A[I] is positive, indicates that the number i + 1 does not appear in the array.

This method only traverses two arrays to complete, without consuming the rest of the space.

1 classSolution {2  Public:3vector<int> Finddisappearednumbers (vector<int>&nums) {4         intLen =nums.size ();5vector<int>Res;6          for(inti =0; i < Len; i++) {7             intindex = ABS (Nums[i])-1;8             if(Nums[index] >0) {9Nums[index] *=-1;Ten             } One         } A          -          for(inti =0; i < Len; i++) { -             if(Nums[i] >0) { theRes.push_back (i +1); -             } -         } -          +         returnRes; -     } +};

Leetcode 448 Find All Numbers disappeared in an Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.