C++ Optimal solution


  • 5
    4
    vector< int > twoSum( vector< int > & nums, int target )
    {
        unordered_map< int, int > umap;
        for( int i = 0; i < nums.size(); ++i )
        { 
            if( umap.count( nums[i] ) ) return vector< int>( 2 ) = { umap[nums[i]], i };
            umap[target - nums[i]] = i;
        }
        return vector< int>();
    }

  • 0
    H

    Can you tell me what the last line "return vector< int>();" is for?

    My code won't be accepted without such a line.

    Thx!


  • 0
    R

    It's for function return type. You can also do something like
    vector<int> res;
    return res;


Log in to reply
 

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