Q: Given a set of distinct integers, nums, return all possible subsets.
Note: Elements in a subset must be in non-descending order; The solution set must not contain duplicate subsets.
A: recursion problem
Java version 1:
import java.util.Arrays;public class Solution { public List
> subsets(int[] nums) { List
> ans = new ArrayList
>(); if (nums == null) return ans; Arrays.sort(nums); List ele = new ArrayList (); addElement(ans, ele, nums, 0, nums.length-1); return ans; } public void addElement(List
> ans, List ele, int[] nums, int start, int end){ if (start > end){ List tmp = new ArrayList (ele); ans.add(tmp); return; } for(int i = start; i <= end; i++){ ele.add(nums[i]); addElement(ans, ele, nums, i+1, end); ele.remove(ele.size()-1); } List tmp = new ArrayList (ele); ans.add(tmp); return; }}