4ms c++ without recursion


  • 0
    A
    vector<vector<int>> subsets(vector<int>& nums) {
    
        sort(nums.begin(),nums.end());
    
        vector<int> s;
        vector<vector<int>> result(1,s);
        int n = nums.size(),j;
        
        for(int i = 0 ; i+1<1<<n;i++)
        {
            for(j = 0; (i>>j)&1;j++) s.pop_back();
            s.push_back(nums[n - j - 1]);
            result.push_back(s);
        }
        
        return result;
    }

  • 0
    D

    Great idea! but how the result of mine's time is 8ms.


  • 0
    W

    The time of the first test is 4ms, however follow-up tests have 8ms time. I have no idea about that.


Log in to reply
 

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