LeetCode 78 Subsets

标签: 回溯法 LeetCode 发布于:2022-02-22 16:58:03 编辑于:2022-02-22 16:58:03 浏览量:359

概述

https://leetcode.com/problems/subsets/

回溯法

class Solution {
public:
    vector<vector<int>> ans;
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> s;
        backtrack(nums, s, 0);
        return ans;
    }
    
    void backtrack(vector<int>& nums, vector<int>& s, int i) {
        if (i == nums.size()) {
            ans.push_back(s);
            return;
        }
        
        backtrack(nums, s, i + 1);
        s.push_back(nums[i]);
        backtrack(nums, s, i + 1);
        s.pop_back();
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/