Created
October 8, 2021 05:57
-
-
Save stellaqx/86e783b3aafc97a38e2be412f2c8c0d6 to your computer and use it in GitHub Desktop.
subsets (java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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