-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPermutation.java
More file actions
37 lines (28 loc) · 968 Bytes
/
Permutation.java
File metadata and controls
37 lines (28 loc) · 968 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
33
34
35
36
37
/**
* LeetCode 46 https://leetcode.com/problems/permutations/submissions/
*/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> track = new LinkedList<>();
boolean[] used = new boolean[nums.length];
backtrack(nums,track,used, res);
return res;
}
void backtrack(int[] nums, LinkedList<Integer> track, boolean[] used, List<List<Integer>> res) {
if (track.size() == nums.length) {
res.add(new LinkedList(track));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) {
continue;
}
track.add(nums[i]);
used[i] = true;
backtrack(nums, track, used, res);
track.removeLast();
used[i] = false;
}
}
}