-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0046_Permutations.java
More file actions
61 lines (54 loc) · 1.54 KB
/
P0046_Permutations.java
File metadata and controls
61 lines (54 loc) · 1.54 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package yyl.leetcode.p00;
import java.util.ArrayList;
import java.util.List;
/**
* <h3>全排列</h3><br>
* 给定一个没有重复数字的序列,返回其所有可能的全排列。<br>
* 示例:<br>
* 输入: [1,2,3]<br>
* 输出:<br>
*
* <pre>
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
* </pre>
*/
public class P0046_Permutations {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.permute(new int[] {1, 2, 3}));
}
static class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
permute(nums, 0, result);
return result;
}
private void permute(int[] nums, int level, List<List<Integer>> result) {
if (level == nums.length - 1) {
List<Integer> item = new ArrayList<>();
for (int num : nums) {
item.add(num);
}
result.add(item);
} else {
for (int i = level; i < nums.length; i++) {
swap(nums, level, i);
permute(nums, level + 1, result);
swap(nums, level, i);
}
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}