-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15a012f
commit d06e68b
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class Solution { | ||
public List<List<Integer>> permuteUnique(int[] num) { | ||
List<List<Integer>> result = new ArrayList<List<Integer>>(); | ||
if (num.length == 0) return result; | ||
boolean[] used = new boolean[num.length]; | ||
List<Integer> list = new ArrayList<Integer>(); | ||
Arrays.sort(num); | ||
dfs(num, num.length, list, used, result); | ||
return result; | ||
} | ||
|
||
private void dfs(int[] num, int n, List<Integer> list, boolean[] used, List<List<Integer>> result) { | ||
if (list.size() == n) { | ||
result.add(new ArrayList<Integer>(list)); | ||
} else { | ||
for (int i = 0; i < n; i++) { | ||
if (used[i] == false) { | ||
used[i] = true; | ||
list.add(num[i]); | ||
dfs(num, n, list, used, result); | ||
used[i] = false; | ||
list.remove(list.size() - 1); | ||
while (i < n - 1 && num[i] == num[i+1]) i++; | ||
} | ||
} | ||
} | ||
} | ||
} |