-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
30 lines (28 loc) · 780 Bytes
/
Permutations.java
File metadata and controls
30 lines (28 loc) · 780 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
class Solution {
public List<List<Integer>> permute(int[] nums) {
ArrayList<List<Integer>> res = new ArrayList<>();
per(nums, 0, res);
return res;
}
public void per(int[] nums, int st, ArrayList<List<Integer>> res) {
if (st == nums.length) {
List<Integer> pr = new ArrayList<>();
for (int i : nums) {
pr.add(i);
}
res.add(pr);
return;
}
for (int i = st; i < nums.length; i++) {
swap(nums, st, i);
per(nums, st + 1, res);
swap(nums, st, i);
}
}
public void swap(int[] nums, int i, int j) {
int tem = nums[i];
nums[i] = nums[j];
nums[j] = tem;
return;
}
}