Skip to content

Commit 975d73e

Browse files
committed
[add] LeetCode 46. Permutations
1 parent 8e5e7f5 commit 975d73e

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

algorithms-leetcode/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Leetcode problems classified by company:
5151
|39|Combination Sum|Medium|Backtracking||
5252
|41|First Missing Positive|Hard|Hash Table|一题多解|
5353
|45|Jump Game II|Medium|Dynamic Programming/Greedy|一题多解|
54+
|46|Permutations|Medium|Backtracking||
5455
|53|Maximum Subarray|Medium|Dynamic Programming/divide and conquer|一题多解|
5556
|55|Jump Game|Medium|Greedy||
5657
|62|Unique Paths|Medium|||

algorithms-leetcode/src/main/java/com/brianway/learning/algorithms/leetcode/medium/LongestSubstringWithoutRepeatingCharacters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* LeetCode 3. Longest Substring Without Repeating Characters
88
* Question: https://leetcode.com/problems/longest-substring-without-repeating-characters/
99
* 关键题设: 无
10-
* * @auther brian
1110
*
11+
* @auther brian
1212
* @since 2022/11/30 23:37
1313
*/
1414
public class LongestSubstringWithoutRepeatingCharacters {
@@ -19,7 +19,7 @@ public int lengthOfLongestSubstring(String s) {
1919

2020
/**
2121
* 滑动窗口+hash表
22-
*
22+
* <p>
2323
* 测试用例:"tmmzuxt"
2424
*/
2525
public class LongestSubstringWithoutRepeatingCharacters0 extends LongestSubstringWithoutRepeatingCharacters {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.brianway.learning.algorithms.leetcode.medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/**
9+
* LeetCode 46. Permutations
10+
* Question: https://leetcode.com/problems/permutations/
11+
* 关键题设: 无
12+
*
13+
* @auther brian
14+
* @since 2022/12/1 23:08
15+
*/
16+
public class Permutations {
17+
18+
public List<List<Integer>> permute(int[] nums) {
19+
return null;
20+
}
21+
22+
public class Permutations0 extends Permutations {
23+
@Override
24+
public List<List<Integer>> permute(int[] nums) {
25+
List<List<Integer>> result = new ArrayList<>();
26+
List<Integer> numbers = new ArrayList<Integer>();
27+
for (int num : nums) {
28+
numbers.add(num);
29+
}
30+
31+
backtracking(result, new LinkedList<>(), numbers, 0);
32+
return result;
33+
}
34+
35+
/**
36+
* @param result
37+
* @param path
38+
* @param nums
39+
* @param start
40+
*/
41+
public void backtracking(List<List<Integer>> result, LinkedList<Integer> path, List<Integer> nums, int start) {
42+
if (start == nums.size()) {
43+
result.add(new ArrayList<>(path));
44+
}
45+
46+
for (int i = start; i < nums.size(); i++) {
47+
path.push(nums.get(i));
48+
Collections.swap(nums, i, start);
49+
backtracking(result, path, nums, start + 1);
50+
Collections.swap(nums, start, i);
51+
path.pop();
52+
}
53+
}
54+
55+
}
56+
}

0 commit comments

Comments
 (0)