-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.java
More file actions
36 lines (29 loc) · 835 Bytes
/
Permutations.java
File metadata and controls
36 lines (29 loc) · 835 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
33
34
35
36
package com.mirraico.leetcode;
import java.util.*;
public class Permutations {
public void convert(int[] nums, int pos, List<List<Integer>> ans) {
List<Integer> tmpAns = new ArrayList<>();
for(int i = 0; i < nums.length; i++) {
tmpAns.add(nums[i]);
}
ans.add(tmpAns);
if(pos >= nums.length - 1) return;
int tmp;
for(int i = pos; i < nums.length - 1; i++) {
for(int j = i + 1; j < nums.length; j++) {
tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
this.convert(nums, i + 1, ans);
tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
}
}
}
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
this.convert(nums, 0, ans);
return ans;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
new Solution().permute(nums);
}
}