leetcode78 子集

code

class Solution {
     

    List<List<Integer>> res = new ArrayList<>();

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

    private void backtrack(int id, int[] nums, List<Integer> path){
     
        if (id == nums.length){
     
            res.add(new ArrayList<>(path));
            return;
        }

        path.add(nums[id]);
        backtrack(id + 1, nums, path);
        path.remove(path.size() - 1);
        backtrack(id + 1, nums, path);
    }
}

你可能感兴趣的:(回溯)