leetcode刷题:子集(递归)(回溯法)

leetcode刷题:子集(递归)(回溯法)_第1张图片

class Solution {
    public List> subsets(int[] nums) {
        List> res = new ArrayList<>();
        backtrack(0, nums, res, new ArrayList());
        return res;

    }
    private void backtrack(int i, int[] nums, List> res, ArrayList tmp) {
        res.add(new ArrayList<>(tmp));//添加空集
        for (int j = i; j < nums.length; j++) {
            tmp.add(nums[j]);
            backtrack(j + 1, nums, res, tmp);
            tmp.remove(tmp.size() - 1);
        }
    }
}

你可能感兴趣的:(LeetCode刷题)