zoukankan      html  css  js  c++  java
  • 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 that do not appear in this array.

    Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

    Example:

    Input:
    [4,3,2,7,8,2,3,1]
    
    Output:
    [5,6]

    解题思路:

    1. class Solution {  
    2. public:  
    3.     vector<int> findDisappearedNumbers(vector<int>& nums) {  
    4.           
    5.         unordered_map<int,int> temp;  
    6.         vector<int> ret;  
    7.         for(int i=0;i<nums.size();i++)  
    8.             temp[nums[i]]++;  
    9.           
    10.         for(int i=1;i<=nums.size();i++)  
    11.             if(temp.count(i) == 0)  
    12.                 ret.push_back(i);  
    13.           
    14.         return ret;  
    15.     }  
    16. };  
  • 相关阅读:
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
    Careercup
  • 原文地址:https://www.cnblogs.com/liangyc/p/8793982.html
Copyright © 2011-2022 走看看