448 Find all Numbers disappeared in an array finds missing numbers from all arrays

Source: Internet
Author: User

Given an array of integers with a range in 1≤a[i]≤n (n = array size), the elements in the array appear two times, and others appear only once.
Find all numbers in the [1, n] range that do not appear in the array.
Can you accomplish this task without using extra space and time complexity O (n)? You can assume that the returned array is not counted in the extra space.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
See: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/

C++:

Method One:

Class Solution {public:    vector<int> finddisappearednumbers (vector<int>& nums) {        vector< int> Res;        for (int i = 0; i < nums.size (); ++i)        {            int idx = ABS (Nums[i])-1;            NUMS[IDX] = (Nums[idx] > 0)? -NUMS[IDX]: Nums[idx];        }        for (int i = 0; i < nums.size (); ++i)        {            if (Nums[i] > 0)             {                Res.push_back (i + 1);            }        }        return res;    }};

Method Two:

Class Solution {public:    vector<int> finddisappearednumbers (vector<int>& nums)    {        vector <int> Res;        for (int i = 0; i < nums.size (); ++i)        {            if (nums[i]! = Nums[nums[i]-1])             {                swap (nums[i], nums[nums[i] -1]);                -----;}        }        for (int i = 0; i < nums.size (); ++i)        {            if (nums[i]! = i + 1)            {                Res.push_back (i + 1);            }        }        return res;}    ;

Reference: https://www.cnblogs.com/grandyang/p/6222149.html

448 Find all Numbers disappeared in an array finds missing numbers from all arrays

Related Article

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.