-
Notifications
You must be signed in to change notification settings - Fork 0
/
0046_Permutations.java
35 lines (29 loc) · 1.13 KB
/
0046_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
30
31
32
33
34
35
class Solution {
private List<List<Integer>> permute(int[] nums, int start) {
ArrayList<List<Integer>> result = new ArrayList<>();
// base case
if(start == nums.length) {
result.add(new ArrayList<Integer>());
return result;
}
for(int i = start; i < nums.length; i++) {
// place nums[i] into the starting index (swap nums[i] into nums[start])
int temp = nums[i];
nums[i] = nums[start];
nums[start] = temp;
// add each permutation that starts with nums[0] (prev. nums[i])
for(List<Integer> permutation : permute(nums, start + 1)) {
permutation.add(0, nums[start]); // insert nums[start] at the beginning of the result
result.add(permutation); // add the result
}
//revert swap
temp = nums[i];
nums[i] = nums[start];
nums[start] = temp;
}
return result;
}
public List<List<Integer>> permute(int[] nums) {
return permute(nums, 0);
}
}