-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain83.java
More file actions
29 lines (26 loc) · 803 Bytes
/
Main83.java
File metadata and controls
29 lines (26 loc) · 803 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
package JZOfferTuJi;
import java.util.ArrayList;
import java.util.List;
public class Main83 {
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
boolean[] used = new boolean[nums.length];
dfs(nums, new ArrayList<Integer>(), used);
return ans;
}
private void dfs(int[] nums, ArrayList<Integer> res, boolean[] used) {
if(res.size() == nums.length) {
ans.add(new ArrayList<>(res));
return;
}
for (int i = 0; i < nums.length; i++) {
if(!used[i]) {
res.add(nums[i]);
used[i] = true;
dfs(nums, res, used);
used[i] = false;
res.remove(res.size() - 1);
}
}
}
}