-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46. Permutations.java
More file actions
32 lines (30 loc) · 872 Bytes
/
46. Permutations.java
File metadata and controls
32 lines (30 loc) · 872 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
//https://leetcode.com/problems/permutations/description/
class Solution {
List<List<Integer>> ans = new ArrayList<>();
void solve(int[] nums, int[] per, int c) {
if (c == nums.length) {
List<Integer> permutation = new ArrayList<>();
for (int num : per) {
permutation.add(num);
}
ans.add(permutation);
return;
}
for (int i = 0; i < nums.length; i++) {
if (per[i] == 11) {
per[i] = nums[c];
solve(nums, per, c + 1);
per[i] = 11;
}
}
}
public List<List<Integer>> permute(int[] nums) {
int n = nums.length;
int[] per = new int[n];
for (int i = 0; i < n; i++) {
per[i] = 11;
}
solve(nums, per, 0);
return ans;
}
}