-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations_leet.java
More file actions
30 lines (24 loc) · 925 Bytes
/
Copy pathPermutations_leet.java
File metadata and controls
30 lines (24 loc) · 925 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
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> r = new ArrayList<>();
List<Integer> chosen = new ArrayList<Integer>();
List<Integer> input = Arrays.stream(nums).boxed().collect(Collectors.toList());
permuteHelper(input,r,chosen);
return r;
}
static void permuteHelper(List<Integer> input,List<List<Integer>> r, List<Integer> chosen){
if(input.size()==0){
r.add(new ArrayList<>(chosen));
return;
}
for(int i=0;i<input.size();i++){
int c = input.get(i);
input.remove(i);
chosen.add(c);
permuteHelper(input,r,chosen);
input.add(i,c);
if(chosen.size()>0)
chosen.remove(chosen.size()-1);
}
}
}