C++ backtack solution, very concise.


  • 1
    class Solution {
    public:
      vector<vector<int> > subsets(vector<int>& nums) {
        vector<vector<int> > ret;
        vector<int> sub;
        
        backtack(nums, sub, ret, 0);
        return ret;
      }
    
      void backtack(vector<int> &nums, vector<int> &sub,  vector<vector<int> > &ret, int index)
      {
        ret.push_back(sub);
        for (int i = index; i < nums.size(); i ++)
        {
          sub.push_back(nums[i]);
          backtack(nums, sub, ret, i + 1);
          sub.pop_back();
        }
      }
    };
    

Log in to reply
 

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