forked from Ma63d/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.java
More file actions
27 lines (27 loc) · 1.01 KB
/
permutations.java
File metadata and controls
27 lines (27 loc) · 1.01 KB
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
public class Solution {
List<List<Integer>> result = new ArrayList<List<Integer>>();
public List<List<Integer>> permute(int[] nums) {
this.result = new ArrayList<List<Integer>>();
ArrayList<Integer> numsToUse = new ArrayList<Integer>();
for(int i = 0;i < nums.length;i++){
numsToUse.add(nums[i]);
}
ArrayList<Integer> alreadyAdd = new ArrayList<Integer>();
addNums(alreadyAdd,numsToUse);
return this.result;
}
public void addNums(List<Integer> alreadyAdd,List<Integer> left){
if(left.isEmpty()){
List<Integer> newResult = new ArrayList<Integer>(alreadyAdd);
this.result.add(newResult);
}else{
for(int i = 0;i < left.size();i++){
int k = (int)left.remove(i);
alreadyAdd.add(k);
addNums(alreadyAdd,left);
alreadyAdd.remove(alreadyAdd.size()-1);
left.add(i,k);
}
}
}
}