Repository navigation
Expand file tree
/
Copy pathPermutations.java
More file actions
66 lines (51 loc) · 1.63 KB
/
Copy pathPermutations.java
File metadata and controls
66 lines (51 loc) · 1.63 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
62
63
64
65
/*
Given a list of numbers, return all possible permutations.
Example
For nums [1,2,3], the permutaions are:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
Challenge
Do it without recursion
Tags Expand
Recursion Search
Thinking Process:
1. Very similar idea: choose or not choose (1 / 0)
A key point is: when jumpped into next level of recursion, the 'list' will surely be filled up until it reach the max length.
That is: when 'not choose', the empty seat will be filled eventually with points not existed in 'list'.
2. The recursion does not end before the list is filled.
3. A for loop is doiong the filling of blank. Any order/combination will occur.
*/
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
if (nums == null || nums.size() == 0) {
return rst;
}
ArrayList<Integer> list = new ArrayList<Integer>();
helper(rst, list, nums);
return rst;
}
public void helper(ArrayList<ArrayList<Integer>> rst, ArrayList<Integer> list, ArrayList<Integer> nums) {
if (list.size() == nums.size()) {
rst.add(new ArrayList<Integer>(list));
return ;
}
for (int i = 0; i < nums.size(); i++) {
if (!list.contains(nums.get(i))) {
list.add(nums.get(i));
helper(rst, list, nums);
list.remove(list.size() - 1);
}
}
}
}