Skip to content

Commit bca63e2

Browse files
add note for 46
1 parent 1945931 commit bca63e2

File tree

1 file changed

+1
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+1
-31
lines changed

src/main/java/com/fishercoder/solutions/_46.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
public class _46 {
2525

2626
public static class Solution1 {
27-
//this solution has a backtracking function that its return type is not void
2827
public List<List<Integer>> permute(int[] nums) {
2928
List<List<Integer>> result = new ArrayList();
3029
result.add(new ArrayList<>());
@@ -37,7 +36,7 @@ private List<List<Integer>> backtracking(List<List<Integer>> result, int[] nums,
3736
}
3837
List<List<Integer>> newResult = new ArrayList();
3938
for (List<Integer> eachList : result) {
40-
for (int i = 0; i <= eachList.size(); i++) {
39+
for (int i = 0; i <= eachList.size(); i++) {//attn: i starts from 0
4140
List<Integer> newList = new ArrayList(eachList);
4241
newList.add(i, nums[pos]);
4342
newResult.add(newList);
@@ -48,33 +47,4 @@ private List<List<Integer>> backtracking(List<List<Integer>> result, int[] nums,
4847
}
4948
}
5049

51-
public static class Solution2 {
52-
public List<List<Integer>> permute(int[] nums) {
53-
List<List<Integer>> result = new ArrayList();
54-
result.add(new ArrayList<>());
55-
recursive(result, nums, 0);
56-
return result;
57-
}
58-
59-
private void recursive(List<List<Integer>> result, int[] nums, int pos) {
60-
if (pos == nums.length) {
61-
return;
62-
}
63-
List<List<Integer>> newResult = new ArrayList();
64-
for (List<Integer> eachList : result) {
65-
for (int i = 0; i <= eachList.size(); i++) {
66-
List<Integer> newList = new ArrayList(eachList);
67-
newList.add(i, nums[pos]);
68-
newResult.add(newList);
69-
}
70-
}
71-
/**You'll have to use the two lines, instead of this line: result = newResult; otherwise, it won't work!!!*/
72-
result.clear();
73-
result.addAll(newResult);
74-
75-
//then recursion
76-
recursive(result, nums, pos + 1);
77-
}
78-
}
79-
8050
}

0 commit comments

Comments
 (0)