马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List <Integer> > res = new ArrayList();
helper(0, nums, new ArrayList<Integer>(), res);
return res;
}
public void helper(int index, int[]nums, List<Integer> cur, List<List<Integer>> res){
res.add(new ArrayList<>(cur));
for(int i = index; i < nums.length; i++){
cur.add(nums[i]);
helper(i+1, nums, cur,res);
cur.remove(cur.size() -1);
}
}
}
|