Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
65 lines (60 loc) · 1.81 KB

File metadata and controls

65 lines (60 loc) · 1.81 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package problems;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class LeetCode46 {
public List<List<Integer>> permute(int[] nums) {
int length=nums.length;
List<List<Integer>> ans=new ArrayList<>();
if(length==0){
return ans;
}
Deque<Integer> path = new ArrayDeque<>();
boolean used[]=new boolean[length];
dfs(nums,length,0,path,used,ans);
return ans;
}
// 深搜
private void dfs(int[] nums, int length, int depth, Deque<Integer> path, boolean[] used, List<List<Integer>> ans) {
if(depth==length){
ans.add(new ArrayList<>(path));
return;
}
for(int i=0;i<length;i++){
if(used[i]){
continue;
}
path.addLast(nums[i]);
used[i]=true;
dfs(nums,length,depth+1,path,used,ans);
path.removeLast();
used[i]=false;
}
}
}
class LeetCode46_1{
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ret = new ArrayList<>();
Deque<Integer> path = new ArrayDeque<>();
int length = nums.length;
boolean[] used = new boolean[length];
dfs(nums, used, length, path, ret);
return ret;
}
void dfs(int[] nums, boolean[] used, int length, Deque<Integer> path, List<List<Integer>> ret){
if(path.size() == length){
ret.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < length; i++) {
if(!used[i]){
used[i] = true;
path.add(nums[i]);
dfs(nums, used, length, path, ret);
path.removeLast();
used[i] = false;
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.