Lc_78子集

package com.example.leetcode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @description: 78. 子集
 * 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
 * <p>
 * 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
 * <p>
 * <p>
 * <p>
 * 示例 1:
 * <p>
 * 输入:nums = [1,2,3]
 * 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
 * 示例 2:
 * <p>
 * 输入:nums = [0]
 * 输出:[[],[0]]
 * <p>
 * <p>
 * 提示:
 * <p>
 * 1 <= nums.length <= 10
 * -10 <= nums[i] <= 10
 * nums 中的所有元素 互不相同
 * @author: licm
 * @create: 2021-05-10 09:46
 **/
public class Lc_78子集 {
    public static List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<Integer> path = new ArrayDeque<>();
        backtracking(nums, 0, path, res);
        return res;
    }

    static void backtracking(int[] nums, int startIndex, Deque<Integer> path, List<List<Integer>> res) {
        res.add(new ArrayList<>(path));
        if (startIndex >= nums.length) {
            return;
        }
        for (int i = startIndex; i < nums.length; i++) {
            path.addLast(nums[i]);
            /**
             * 为了不重复 索引 i+1
             */
            backtracking(nums, i + 1, path, res);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        List<List<Integer>> res = subsets(nums);
        res.forEach(m -> {
            for (Integer r : m) {
                System.out.print(r + "");
            }
            System.out.println();
        });
        System.out.println();
    }

}




























不会,我可以学;落后,我可以追赶;跌倒,我可以站起来!
原文地址:https://www.cnblogs.com/xiaoshahai/p/14749930.html