Skip to content

Commit 5a1441f

Browse files
refactor 46
1 parent 50cf51e commit 5a1441f

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ Your ideas/fixes/algorithms are more than welcome!
573573
|49|[Group Anagrams](https://leetcode.com/problems/group-anagrams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_49.java)|O(m*logn)|O(m*n)|Medium| HashMap
574574
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_48.java)|O(n^2)|O(1)| Medium | Array
575575
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_47.java)|O(n*n!)|O(n)|Medium|Backtracking
576-
|46|[Permutations](https://leetcode.com/problems/permutations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_46.java)|O(n*n!)|O(n)|Medium|Backtracking
576+
|46|[Permutations](https://leetcode.com/problems/permutations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_46.java)| O(n*n!) | O(n) | Medium | Backtracking
577577
|45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_45.java)|O(?)|O(?)|Hard|
578578
|44|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_44.java)|O(m*n)|O(m*n)|Hard| Backtracking, DP, Greedy, String
579579
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_43.java)|O(n)|O(1)|Medium| Array, String

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**Given a collection of distinct numbers, return all possible permutations.
6+
/**
7+
* 46. Permutations
8+
*
9+
* Given a collection of distinct numbers, return all possible permutations.
710
811
For example,
912
[1,2,3] have the following permutations:
@@ -14,18 +17,21 @@
1417
[2,3,1],
1518
[3,1,2],
1619
[3,2,1]
17-
]*/
20+
]
21+
22+
*/
23+
1824
public class _46 {
19-
static class AcceptedSolution {
25+
26+
public static class Solution1 {
2027
//this solution has a backtracking function that has a return type
21-
public static List<List<Integer>> permute(int[] nums) {
28+
public List<List<Integer>> permute(int[] nums) {
2229
List<List<Integer>> result = new ArrayList();
23-
List<Integer> init = new ArrayList<>();
24-
result.add(init);
30+
result.add(new ArrayList<>());
2531
return backtracking(result, nums, 0);
2632
}
2733

28-
private static List<List<Integer>> backtracking(List<List<Integer>> result, int[] nums, int pos) {
34+
private List<List<Integer>> backtracking(List<List<Integer>> result, int[] nums, int pos) {
2935
if (pos == nums.length) {
3036
return result;
3137
}
@@ -42,16 +48,15 @@ private static List<List<Integer>> backtracking(List<List<Integer>> result, int[
4248
}
4349
}
4450

45-
static class AcceptedSolutionWithVoidType {
46-
public static List<List<Integer>> permute(int[] nums) {
51+
public static class Solution2 {
52+
public List<List<Integer>> permute(int[] nums) {
4753
List<List<Integer>> result = new ArrayList();
48-
List<Integer> init = new ArrayList<>();
49-
result.add(init);
54+
result.add(new ArrayList<>());
5055
recursive(result, nums, 0);
5156
return result;
5257
}
5358

54-
private static void recursive(List<List<Integer>> result, int[] nums, int pos) {
59+
private void recursive(List<List<Integer>> result, int[] nums, int pos) {
5560
if (pos == nums.length) {
5661
return;
5762
}
@@ -72,9 +77,4 @@ private static void recursive(List<List<Integer>> result, int[] nums, int pos) {
7277
}
7378
}
7479

75-
public static void main(String... args) {
76-
int[] nums = new int[]{1, 2, 2};
77-
78-
}
79-
8080
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy