Saturday, June 25, 2016

78. Subsets

Classic backtracking solution.

1:  class Solution {  
2:  public:  
3:    vector<vector<int>> subsets(vector<int>& nums) {  
4:      vector<vector<int>> res;  
5:      vector<int> sol;  
6:      res.push_back(sol);  
7:      helper(nums, 0, sol, res);  
8:      return res;  
9:    }  
10:    void helper(vector<int>& nums, int start, vector<int> &sol, vector<vector<int>> &res) {  
11:      if (start == nums.size()) return;  
12:      for (int i = start; i< nums.size(); i++) {  
13:        sol.push_back(nums[i]);  
14:        res.push_back(sol);  
15:        helper(nums, i+1, sol, res);  
16:        sol.pop_back();  
17:      }  
18:    }  
19:  };  

No comments:

Post a Comment