78. Subsets

    xiaoxiao2021-03-25  123

    Given a set of distinct integers, nums, return all possible subsets.

    Note: The solution set must not contain duplicate subsets.

    For example, If nums = [1,2,3], a solution is:

    [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]

    Subscribe to see which companies asked this question.

    public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> re = new ArrayList<List<Integer>>(); if (nums.length == 0) return re; boolean[] data = new boolean[nums.length]; backtracking(data, nums, 0, nums.length, re); return re; } static void backtracking(boolean[] data, int[] nums, int deep, int length, List<List<Integer>> re) { if (deep == length) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < length; ++i) { if (data[i]) temp.add(nums[i]); } re.add(temp); return; } data[deep] = true; backtracking(data, nums, deep + 1, length, re); data[deep] = false; backtracking(data, nums, deep + 1, length, re); } }

    转载请注明原文地址: https://ju.6miu.com/read-12698.html

    最新回复(0)