forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46-Permutations.java
More file actions
32 lines (27 loc) · 829 Bytes
/
46-Permutations.java
File metadata and controls
32 lines (27 loc) · 829 Bytes
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
import java.util.ArrayList;
import java.util.Arrays;
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
function(ans, nums, 0);
return ans;
}
public void function(List<List<Integer>> ans, int[] arr, int start) {
if (start == arr.length) {
List<Integer> list = new ArrayList();
for (int i = 0; i < arr.length; i++) list.add(arr[i]);
ans.add(list);
return;
}
for (int i = start; i < arr.length; i++) {
swap(arr, start, i);
function(ans, arr, start + 1);
swap(arr, start, i);
}
}
public void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}