给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
res.add(new ArrayList<>());
for (int i = 0; i < nums.length; i++) {
int size = res.size();
//循环返回数组 往之前返回数组添加当前循环的数 生成新的数组 只能提前获取数组长度 不然数组一直递增 死循环
for (int j = 0; j < size; j++) {
//获取之前生成的数组 并new出新对象
List<Integer> resC = new ArrayList<>(res.get(j));
//之前生成数组加入当前循环的数
resC.add(nums[i]);
res.add(resC);
}
}
return res;
}