Created
March 3, 2014 06:09
-
-
Save rayjcwu/9319335 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { | |
| public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) { | |
| Arrays.sort(S); | |
| ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); | |
| boolean []selected = new boolean[S.length]; | |
| subset(S, 0, selected, ans); | |
| return ans; | |
| } | |
| private void subset(int[] S, int i, boolean []selected, ArrayList<ArrayList<Integer>> ans) { | |
| if (i == S.length) { | |
| ArrayList<Integer> t = new ArrayList<Integer>(); | |
| for (int j = 0; j < selected.length; j++) { | |
| if (selected[j]) | |
| t.add(S[j]); | |
| } | |
| ans.add(t); | |
| } else { | |
| int j = i + 1; | |
| while (j < selected.length && S[j] == S[i]) { | |
| j++; | |
| } | |
| selected[i] = true; | |
| subset(S, i + 1, selected, ans); | |
| selected[i] = false; | |
| subset(S, j, selected, ans); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment