C++ O(n) time and O(n) space


  • 0
    L
    class Solution {
    public:
        vector<int> findDisappearedNumbers ( vector<int>& nums ) {
    		vector<int> temp(nums.size(),0);
    		for ( size_t i = 0; i < nums.size ( ); i++ )
    		{
    			temp[nums[i]-1]++;
    
    		}
    		int j = 0;
    		for ( size_t i = 0; i < temp.size ( ); i++ )
    		{
    			if ( temp [ i ] == 0 )
    			{
    				nums [ j ] = i + 1;
    				j++;
    			}
    		}
    		for ( size_t i = j; i < temp.size(); i++ )
    		{
    			nums.pop_back ( );
    
    		}
    		return nums;
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.