- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy path46_Permutations.java
29 lines (24 loc) · 777 Bytes
/
46_Permutations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
classSolution {
publicList<List<Integer>> permute(int[] nums) {
if (nums == null || nums.length == 0) {
returnCollections.emptyList();
}
List<List<Integer>> result = newArrayList<>();
helper(nums, result, newArrayList<>());
returnresult;
}
privatevoidhelper(int[] nums, List<List<Integer>> result, List<Integer> temp) {
if (temp.size() == nums.length) {
result.add(newArrayList<>(temp));
return;
}
for (inti = 0; i < nums.length; i++) {
if (temp.contains(nums[i])) {
continue;
}
temp.add(nums[i]);
helper(nums, result, temp);
temp.remove(temp.size() - 1);
}
}
}