448. Find all Numbers disappeared in an array&&645. Set mismatch

Source: Internet
Author: User

Title:

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 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.

645. Set mismatch

The set S originally contains numbers from 1 to n . But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which Results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number of is missing. Return them in the form of an array.

Ideas:

448 and 645 have similar ideas. The common denominator of the two questions is that the size of the elements is [1,n]. 448 required to find the number that did not appear, 645 required to find the number that appeared two times and the number that did not appear.

Because the subscript of an element is [0,n-1], the size of the element minus 1 is the subscript for an element, so you can use the relationship between the element size and the subscript to find a special number.

For 448, traverse the entire array, minus 1 by the size of the element to get the subscript. If the corresponding element of the subscript is positive, it is multiplied by-1, to a negative number, and if the subscript corresponds to a negative element, it is proved that the subscript has occurred once and is not processed. With this traversal, the subscript that corresponds to the positive element is added 1, which is the element that is not present.

For 645, traverse the entire array, minus 1 by the size of the element to get the subscript. If the subscript corresponds to an element that is positive, multiply it by 1 to become negative, and if the subscript corresponds to a negative element, prove that the subscript has occurred once before, and add the subscript +1 to the result. With this traversal, the subscript that corresponds to the positive element is added 1, which is the element that is not present.

Code:

448.

1 classSolution {2  Public:3vector<int> Finddisappearednumbers (vector<int>&nums) {4vector<int>ans;5          for(inti =0; I < (signed) nums.size (); i++) {6             intindex = ABS (Nums[i])-1;7             if(Nums[index] >0)8Nums[index] *=-1;9             ElseTen                 Continue; One         } A          for(inti =0; I < (signed) nums.size (); i++) -             if(Nums[i] >0) -Ans.push_back (i +1); the         returnans; -     } -};

645.

1 classSolution {2  Public:3vector<int> Finderrornums (vector<int>&nums) {4vector<int>ans;5          for(inti =0; I < (signed) nums.size (); i++) {6             intindex = ABS (Nums[i])-1;7             if(Nums[index] >0) {8Nums[index] *=-1;9}Else {TenAns.push_back (Index +1); One             } A         } -          for(inti =0; I < (signed) nums.size (); i++) { -             if(Nums[i] >0) theAns.push_back (i +1); -         } -         returnans; -     } +};

448. Find all Numbers disappeared in an array&&645. Set mismatch

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.