-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations
More file actions
32 lines (25 loc) · 940 Bytes
/
Permutations
File metadata and controls
32 lines (25 loc) · 940 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
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> fans= new ArrayList<>();
List<Integer> pans= new ArrayList<Integer>();
boolean[] visited= new boolean[nums.length];
return sol(fans,pans , nums,visited);
}
static List<List<Integer>> sol(List<List<Integer>> fans, List<Integer> pans,
int[] nums, boolean[] visited){
if(pans.size()== nums.length){
fans.add(new ArrayList<Integer>(pans));
return fans;
}
for(int i=0; i<nums.length; i++){
if(!visited[i]){
visited[i]= true;
pans.add(nums[i]);
sol(fans, pans, nums, visited);
pans.remove(pans.size()-1);
visited[i]= false;
}
}
return fans;
}
}