給定一組不含重復元素的整數數組 nums,返回該數組所有可能的子集(冪集)。
說明:解集不能包含重復的子集。
示例:
輸入: nums = [1,2,3]
輸出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
代碼
class Solution {public List<List<Integer>> subsets(int[] nums) {sets(nums,0,new ArrayList<>());return ress;}public void sets(int[] nums,int start,List<Integer> temp) {ress.add(new ArrayList<>(temp)); //將上一層的路徑加入for(int i=start;i<nums.length;i++)//可以選擇的數字{temp.add(nums[i]);sets(nums, i+1, temp);temp.remove(temp.size()-1);//回溯}}List<List<Integer>> ress=new ArrayList<>();
}