-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.permutations.java
More file actions
33 lines (30 loc) · 815 Bytes
/
Copy path46.permutations.java
File metadata and controls
33 lines (30 loc) · 815 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
import java.util.ArrayList;
import java.util.List;
/*
* @lc app=leetcode id=46 lang=java
*
* [46] Permutations
*/
// @lc code=start
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> perms = new ArrayList<>();
dfs(perms, new ArrayList(), nums);
return perms;
}
public void dfs(List<List<Integer>> rtn, ArrayList<Integer> arr, int[] targets) {
if (arr.size() == targets.length) {
rtn.add(new ArrayList<Integer>(arr));
} else {
for (int i : targets) {
if (arr.contains(i)) {
continue;
}
arr.add(i);
dfs(rtn, arr, targets);
arr.remove(arr.size() - 1);
}
}
}
}
// @lc code=end