给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集

package week_01;

import java.util.ArrayList;
import java.util.List;

/*
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
例子:
输入nums = [1,2,3]
输出:
[
  [3],
  [1],
  [2],
  ......
*/
public class Day_1 {

    public List<List<Integer>> subsets(int nums[]) {

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

        list_1.add(new ArrayList<>());

        for (int i = 0; i < nums.length; i++) {
            int size = list_1.size();
            System.out.println("list_1.size =  " + size + "   " + "list_1 =  " + list_1);
            for (int j = 0; j < size; j++) {
                List<Integer> temp_list = new ArrayList<>(list_1.get(j));

                System.out.print(list_1.get(j) + "   " + "templist  =  " + temp_list + "     ");

                temp_list.add(nums[i]);

                System.out.println("templist  =  " + temp_list);

                list_1.add(temp_list);
            }

            System.out.println();
        }
        return list_1;
    }

    public static void main(String[] args) {
        int arr[] = {1, 2, 3};
//        System.out.println(new Day_1().subsets(arr));
        new Day_1().subsets(arr);
    }
}
    /*
    * 运行结果:
        list_1.size =  1   list_1 =  [[]]
        []   templist  =  []     templist  =  [1]

        list_1.size =  2   list_1 =  [[], [1]]
        []   templist  =  []     templist  =  [2]
        [1]   templist  =  [1]     templist  =  [1, 2]

        list_1.size =  4   list_1 =  [[], [1], [2], [1, 2]]
        []   templist  =  []     templist  =  [3]
        [1]   templist  =  [1]     templist  =  [1, 3]
        [2]   templist  =  [2]     templist  =  [2, 3]
        [1, 2]   templist  =  [1, 2]     templist  =  [1, 2, 3]
        * 
        * 
        * [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]*/

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页