Skip to content

Instantly share code, notes, and snippets.

@stellaqx
Created October 8, 2021 05:57
Show Gist options
  • Save stellaqx/86e783b3aafc97a38e2be412f2c8c0d6 to your computer and use it in GitHub Desktop.
Save stellaqx/86e783b3aafc97a38e2be412f2c8c0d6 to your computer and use it in GitHub Desktop.
subsets (java)
public class Solution {
/**
* @param nums: A set of numbers
* @return: A list of lists
*/
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
return helper(nums, nums.length - 1);
}
public List<List<Integer>> helper(int[] nums, int index) {
List<List<Integer>> res;
if (index < 0) {
res = new ArrayList<List<Integer>>();
res.add(new ArrayList<Integer>());
return res;
}
List<List<Integer>> subs = helper(nums, index - 1);
res = new ArrayList<List<Integer>>(subs);
for (List<Integer> sub: subs) {
List<Integer> s = new ArrayList<Integer>(sub);
s.add(nums[index]);
res.add(s);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment