Fastest C++ 3ms DFS solution


  • 0
    P

    class Solution {
    public:
    vector<vector<int>> subsets(vector<int>& nums) {
    n = nums.size();
    vector<int> ans;
    dfs(-1, nums, ans);
    return res;
    }
    private:
    void dfs(int start, vector<int>& nums, vector<int>& ans){
    res.push_back(ans);
    for (int i = start + 1; i < n; ++i){
    ans.push_back(nums[i]);
    dfs(i, nums, ans);
    ans.pop_back();
    }
    }
    vector<vector<int>> res;
    int n;
    };


  • 0
    C

    please format your code with
    ```
    you code here
    ```


Log in to reply
 

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