Skip to content

Commit 305af19

Browse files
committed
SOlution for: Permutations array
1 parent 52d664d commit 305af19

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/leetcode/Permutations.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Permutations {
7+
8+
public static void main(String[] args) {
9+
Permutations test = new Permutations();
10+
System.out.println(test.permute(new int[]{1,2,3}));
11+
}
12+
13+
public List<List<Integer>> permute(int[] nums) {
14+
List<List<Integer>> list = new ArrayList<>();
15+
helper(nums,0,nums.length-1,list);
16+
return list;
17+
}
18+
19+
public void helper(int[] nums, int l, int tam, List<List<Integer>> list){
20+
if(l==tam){
21+
List<Integer> newList= new ArrayList<>();
22+
for(int i=0;i<=tam;i++){
23+
newList.add(nums[i]);
24+
}
25+
list.add(newList);
26+
}else{
27+
for(int j=l; j<=tam;j++){
28+
nums= swap(nums,l,j);
29+
helper(nums,l+1,tam,list);
30+
nums = swap(nums,l,j);
31+
}
32+
}
33+
}
34+
35+
public int[] swap(int[] nums,int i, int j){
36+
int tem = nums[i];
37+
nums[i]=nums[j];
38+
nums[j]= tem;
39+
return nums;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)