-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPermutations.java
42 lines (35 loc) · 1.08 KB
/
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
36
37
38
39
40
41
42
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/permutations/
public class Permutations {
private final int[] input;
public Permutations(int[] input) {
this.input = input;
}
public List<List<Integer>> solution() {
List<List<Integer>> result = new ArrayList<>();
permutations(input, new ArrayList<>(), result, new boolean[input.length]);
return result;
}
private void permutations(
int[] nums,
List<Integer> dS,
List<List<Integer>> result,
boolean[] freq
) {
if (dS.size() == nums.length) {
result.add(new ArrayList<>(dS));
} else {
for (int i = 0; i < nums.length; i++) {
if (!freq[i]) {
freq[i] = true;
dS.add(nums[i]);
permutations(nums, dS, result, freq);
dS.remove(dS.size() - 1);
freq[i] = false;
}
}
}
}
}