給定一組不含重復元素的整數數組?nums,返回該數組所有可能的子集(冪集)。
說明:解集不能包含重復的子集。
示例:
輸入: nums = [1,2,3]
輸出:
[
? [3],
??[1],
??[2],
??[1,2,3],
??[1,3],
??[2,3],
??[1,2],
??[]
]
思路:簡單搜索,思路見代碼。
class Solution {List<List<Integer>> lists = new ArrayList<>();public List<List<Integer>> subsets(int[] nums) {if(nums == null || nums.length ==0)return lists;List<Integer> list = new ArrayList<>();process(list, nums, 0);return lists;}private void process(List<Integer>list, int[] nums, int start){lists.add(new ArrayList(list));for(int i = start; i < nums.length; i++){list.add(nums[i]);process(list, nums, i+1);list.remove(list.size()-1);}}
}
?