-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProblem_3_Permutations.java
More file actions
33 lines (30 loc) · 1.15 KB
/
Problem_3_Permutations.java
File metadata and controls
33 lines (30 loc) · 1.15 KB
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
package Subsets;
// Problem Statement: Permutations (medium)
// LeetCode Question: 46. Permutations
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Problem_3_Permutations {
public List<List<Integer>> findPermutations (int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Queue<List<Integer>> permutations = new LinkedList<>();
permutations.add(new ArrayList<>());
for (int currentNumber : nums) {
int n = permutations.size();
for (int i = 0; i < n; i++) {
List<Integer> oldPermutation = permutations.poll();
for (int j = 0; j <= oldPermutation.size(); j++) {
List<Integer> newPermutation = new ArrayList<Integer>(oldPermutation);
newPermutation.add(j, currentNumber);
if (newPermutation.size() == nums.length) {
result.add(newPermutation);
} else {
permutations.add(newPermutation);
}
}
}
}
return result;
}
}