Skip to content

Commit 14022db

Browse files
committed
review-8
1 parent f867fb5 commit 14022db

5 files changed

Lines changed: 63 additions & 24 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/backtrack/test36/Solution.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*
4545
* @Auther: zhunn
4646
* @Date: 2020/11/3 13:54
47-
* @Description: 有效的数独:1-哈希数组;2-位运算数组
47+
* @Description: 有效的数独:1-哈希数组(没有空间要求也可以);2-位运算数组;3-二维数组(推荐)
4848
*/
4949
public class Solution {
5050

@@ -105,13 +105,19 @@ public boolean isValidSudoku2(char[][] board) {
105105
return true;
106106
}
107107

108+
/**
109+
* 二维数组,第一维标识位置,第二维标识1-9数字
110+
*
111+
* @param board
112+
* @return
113+
*/
108114
public boolean isValidSudoku1(char[][] board) {
109115
int[][] row = new int[9][10]; // 哈希表存储每一行的每个数是否出现过,默认初始情况下,每一行每一个数都没有出现过
110116
// 整个board有9行,第二维的维数10是为了让下标有9,和数独中的数字9对应。
111117
int[][] col = new int[9][10]; // 存储每一列的每个数是否出现过,默认初始情况下,每一列的每一个数都没有出现过
112118
int[][] box = new int[9][10]; // 存储每一个box的每个数是否出现过,默认初始情况下,在每个box中,每个数都没有出现过。整个board有9个box。
113-
for (int i = 0; i < 9; i++) {
114-
for (int j = 0; j < 9; j++) {
119+
for (int i = 0; i < 9; i++) { //行
120+
for (int j = 0; j < 9; j++) {//列
115121
if (board[i][j] == '.') {
116122
continue;
117123
}

‎src/main/java/com/chen/algorithm/znn/backtrack/test39/Solution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public List<List<Integer>> combinationSum(int[] candidates, int target) {
4343
}
4444
List<List<Integer>> res = new ArrayList<>();
4545
// 排序是剪枝的前提
46-
Arrays.sort(candidates);
46+
//Arrays.sort(candidates);
4747
backtrack(0, target, candidates, new ArrayList<>(), res);
4848
return res;
4949
}

‎src/main/java/com/chen/algorithm/znn/backtrack/test47/Solution.java‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ public List<List<Integer>> permuteUnique(int[] nums) {
3434
Arrays.sort(nums);
3535

3636
boolean[] used = new boolean[nums.length];
37-
dfs(nums, nums.length, 0, used, new ArrayList<>(), res);
37+
backtrack(0, nums.length, nums, used, new ArrayList<>(), res);
3838
return res;
3939
}
4040

41-
private void dfs(int[] nums, int len, int depth, boolean[] used, List<Integer> curList, List<List<Integer>> res) {
41+
private void backtrack(int depth, int len, int[] nums, boolean[] used, List<Integer> curList, List<List<Integer>> res) {
4242
if (depth == len) {
4343
res.add(new ArrayList<>(curList));
4444
return;
4545
}
4646

47-
for (int i = 0; i < len; ++i) {
47+
for (int i = 0; i < len; i++) {
4848
if (used[i]) {
4949
continue;
5050
}
@@ -57,7 +57,7 @@ private void dfs(int[] nums, int len, int depth, boolean[] used, List<Integer> c
5757

5858
curList.add(nums[i]);
5959
used[i] = true;
60-
dfs(nums, len, depth + 1, used, curList, res);
60+
backtrack(depth + 1, len, nums, used, curList, res);
6161
// 回溯部分的代码,和 dfs 之前的代码是对称的
6262
used[i] = false;
6363
curList.remove(curList.size() - 1);
@@ -68,5 +68,37 @@ private void dfs(int[] nums, int len, int depth, boolean[] used, List<Integer> c
6868
public void testCase() {
6969
int[] nums = {1, 1, 2};
7070
System.out.println(JSONObject.toJSONString(permuteUnique(nums)));
71+
System.out.println(JSONObject.toJSONString(permuteUnique2(nums)));
72+
}
73+
74+
public List<List<Integer>> permuteUnique2(int[] nums) {
75+
if (nums == null || nums.length == 0) {
76+
return null;
77+
}
78+
Arrays.sort(nums);
79+
List<List<Integer>> res = new ArrayList<>();
80+
boolean[] used = new boolean[nums.length];
81+
dfs(0, nums.length, nums, used, new ArrayList<>(), res);
82+
return res;
83+
}
84+
85+
private void dfs(int depth, int len, int[] nums, boolean[] used, List<Integer> curList, List<List<Integer>> res) {
86+
if (depth == len) {
87+
res.add(new ArrayList<>(curList));
88+
return;
89+
}
90+
for (int i = 0; i < len; i++) {
91+
if (used[i]) {
92+
continue;
93+
}
94+
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
95+
continue;
96+
}
97+
used[i] = true;
98+
curList.add(nums[i]);
99+
dfs(depth + 1, len, nums, used, curList, res);
100+
curList.remove(curList.size() - 1);
101+
used[i] = false;
102+
}
71103
}
72104
}

‎src/main/java/com/chen/algorithm/znn/backtrack/test90/Solution.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ public class Solution {
3131
public List<List<Integer>> subsetsWithDup(int[] nums) {
3232
List<List<Integer>> res = new ArrayList<>();
3333
Arrays.sort(nums); //排序
34-
backtrack(nums, 0, new ArrayList<>(), res);
34+
backtrack(0, nums, new ArrayList<>(), res);
3535
return res;
3636
}
3737

38-
private void backtrack(int[] nums, int start, ArrayList<Integer> curList, List<List<Integer>> ans) {
39-
ans.add(new ArrayList<>(curList));
38+
private void backtrack(int start, int[] nums, List<Integer> curList, List<List<Integer>> res) {
39+
res.add(new ArrayList<>(curList));
4040
for (int i = start; i < nums.length; i++) {
4141
//和上个数字相等就跳过
4242
if (i > start && nums[i] == nums[i - 1]) {
4343
continue;
4444
}
4545
curList.add(nums[i]);
46-
backtrack(nums, i + 1, curList, ans);
46+
backtrack(i + 1, nums, curList, res);
4747
curList.remove(curList.size() - 1);
4848
}
4949
}

‎src/main/java/com/chen/algorithm/znn/note‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
复刷顺序:
1111
第一类:1) frequency-5
12-
2) bitmap-5
12+
(23) 2) bitmap-5
1313
-----------------------------------------第1天
1414
3) sort-5
1515
4) string-4
1616
5) hash-4
1717
-----------------------------------------第2天
1818
第二类:6) array-18
19+
(60)
1920
-----------------------------------------第3天
2021
7) heap-3
2122
8) stack-7
@@ -25,7 +26,7 @@
2526
10) tree-21
2627
-----------------------------------------第6天
2728
第三类:11) recursion-2
28-
12) bfs-0
29+
(40) 12) bfs-0
2930
13) dfs-1
3031
14) divide-2
3132
15) greedy-2
@@ -159,20 +160,20 @@ divide
159160
169. 多数元素 *** 1-排序;2-哈希表(推荐)放入hashmap计数,判断数量是否大于len/2
160161

161162
greedy
162-
122. 买卖股票的最佳时机 II *** 贪心-迭代计算只要有收益就累加到max,返回max
163+
122. 买卖股票的最佳时机 II *** 贪心-迭代计算只要有收益就累加到max,返回max; 动态规划
163164
55. 跳跃游戏 贪心-倒序遍历,如果n-2下标能到达n-1下标,意味着前面只要有坐标能够到达n-2即可完成跳跃,于是末尾位置更改为n-2,这样构成一个子问题,迭代求解。
164165

165166

166167
backtrack
167-
36. 有效的数独
168-
39. 组合总和 ***
169-
40. 组合总和 II ***
170-
46. 全排列 ***
171-
47. 全排列II ***
172-
51. N皇后 *** ??
173-
79. 单词搜索 ????
174-
78. 子集 ***
175-
90. 子集II***
168+
36. 有效的数独(9X9) 1-哈希数组(占用空间)2-二维数组(推荐),第一维表示位置,第二维表示1-9数字,出现过对应二维数组值置为1标识起来,下次循环等于1直接return false
169+
39. 组合总和 *** 元素可以取多次(结果都不可重复) 回溯法-先排序,递归然后循环并剪枝,最后回溯dfs(start=i)
170+
40. 组合总和 II ***元素只能取一次(结果都不可重复) 回溯法-先排序,递归然后循环并剪枝,最后回溯dfs(start=i+1);if (i > start && nums[i - 1] == nums[i])(剪枝)
171+
46. 全排列 *** 给定数组无重复元素(结果都不可重复) 回溯法-depth、boolen[]
172+
47. 全排列II ***给定数组有重复元素(结果都不可重复) 回溯法-先排序depth、boolen[];i > 0 && nums[i] == nums[i - 1] && !used[i - 1](剪枝)
173+
78. 子集 *** 给定数组无重复元素(结果都不可重复) 回溯法-start,i + 1,递归方法先res.add(new ArrayList<>(curList));
174+
90. 子集II***给定数组有重复元素(结果都不可重复) 回溯法-先排序,剪枝
175+
//51. N皇后 *** ??
176+
79. 单词搜索 ???? 回溯法-定义boolean[][] visited;dfs(int index, char[][] board, String word, int x, int y),判断上|下|左|右
176177
0-1背包问题 /backtrack/ZeroOneBag
177178

178179
dynamic

0 commit comments

Comments
 (0)