Skip to content

Commit f60a6b9

Browse files
committed
要么选择,要不不选择,实现数组全排列
1 parent 1b29e29 commit f60a6b9

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

code/LeetCode/src/backtracking/Permutations.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public class Permutations {
99

1010
public static void main(String[] args) {
1111
Permutations obj = new Permutations();
12-
obj.permuteWithCleanRecursive(new int[]{1, 2, 3});
12+
obj.permuteWithIterate(new int[]{1, 2, 3});
1313
}
1414

15-
public List<List<Integer>> permuteWithCleanRecursive(int[] nums) {
15+
public List<List<Integer>> permuteWithIterate(int[] nums) {
1616
LinkedList<List<Integer>> result = new LinkedList<>();
1717
result.add(new ArrayList<Integer>());
1818
int size;
@@ -56,4 +56,32 @@ private void addItemInAllPosition(int[] nums, int start, List<Integer> list, Lis
5656
addItemInAllPosition(nums, start + 1, newList, result);
5757
}
5858
}
59+
60+
61+
public List<List<Integer>> permuteWithPickupOrNo(int[] nums) {
62+
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
63+
if (nums == null || nums.length == 0) {
64+
return resultList;
65+
}
66+
67+
recursive(nums, new ArrayList<Integer>(), resultList);
68+
69+
return resultList;
70+
}
71+
72+
public void recursive(int[] nums, List<Integer> list, List<List<Integer>> resultList) {
73+
if (list.size() == nums.length) {
74+
resultList.add(new ArrayList<Integer>(list));
75+
return;
76+
}
77+
78+
for(int item: nums) {
79+
if (list.contains(item)) {
80+
continue;
81+
}
82+
list.add(item);
83+
recursive(nums, list, resultList);
84+
list.remove(list.size() - 1);
85+
}
86+
}
5987
}

0 commit comments

Comments
 (0)