Easy Understand 8ms cpp solution using iterative


  • 1
    P
    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) 
        {
          sort(nums.begin(),nums.end()); 
          
          vector<vector<int>> result;
          
          // to inset the [] when first 
          vector<int> empty;
          result.push_back(empty);
          
          for(int i=0;i<nums.size();++i)
          {
              int tempResultSize = result.size();
              for(int j=0;j < tempResultSize;++j)
              {
                vector<int> tempSet;   
                tempSet = result[j];
                tempSet.push_back(nums[i]);
                result.push_back(tempSet);
              }
          }
          
          return result;
          
        }
    };

Log in to reply
 

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