-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 122.java
More file actions
30 lines (26 loc) · 834 Bytes
/
Day 122.java
File metadata and controls
30 lines (26 loc) · 834 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
import java.util.*;
class Solution {
public static ArrayList<ArrayList<Integer>> permuteDist(int[] arr) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
backtrack(0, arr, res);
return res;
}
private static void backtrack(int idx, int[] arr, ArrayList<ArrayList<Integer>> res) {
if (idx == arr.length) {
ArrayList<Integer> temp = new ArrayList<>();
for (int x : arr) temp.add(x);
res.add(temp);
return;
}
for (int i = idx; i < arr.length; i++) {
swap(arr, idx, i);
backtrack(idx + 1, arr, res);
swap(arr, idx, i);
}
}
private static void swap(int[] arr, int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}