update whaifree redo demo更新了whaifree重做演示代码,新增了几个LeetCode题目(46、47、51、491)的解决方案,修改了40题的解法,包括回溯算法的实现和一些测试用例的输出方式。确保了代码的正确性和可读性。

This commit is contained in:
whaifree 2024-08-04 16:37:30 +08:00
parent 567e7f1ff2
commit b20f5f042f
5 changed files with 412 additions and 8 deletions

View File

@ -11,14 +11,9 @@ import java.util.*;
public class LeetCode40 {
public static void main(String[] args) {
new LeetCode40().combinationSum2(new int[]{14,6,25,9,30,20,33,34,28,30,16,12,31,9,9,12,34,16,25,32,8,7,30,12,33,20,21,29,24,17,27,34,11,17,30,6,32,21,27,17,16,8,24,12,12,28,11,33,10,32,22,13,34,18,12}, 27).forEach(
list -> {
list.forEach(
integer -> System.out.print(integer + " ")
);
System.out.println();
}
);
new Solution().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8).forEach(list -> {
System.out.println(list);
});
}
List<List<Integer>> res = new ArrayList<>();
@ -53,4 +48,45 @@ public class LeetCode40 {
nowSum -= candidates[i];
}
}
static class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
/**
*
*
* @param candidates
* @param target
* @return
*/
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
backTracking(candidates, 0, target);
return res;
}
public void backTracking(int[] candidates, int index, int need) {
if (need < 0) {
return;
}
if (need == 0) {
res.add(new ArrayList<>(path));
}
HashSet<Integer> set = new HashSet<>();
for (int i = index; i < candidates.length; i++) {
if (set.contains(candidates[i])) {
continue;
}
set.add(candidates[i]);
path.add(candidates[i]);
backTracking(candidates, i + 1, need - candidates[i]);
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,48 @@
package cn.whaifree.redo.redo_all_240721;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/3 10:19
* @注释
*/
public class LeetCode46 {
@Test
public void test() {
int[] nums = {1,2,3};
Solution solution = new Solution();
solution.permute(nums).forEach(
System.out::println
);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
back(nums);
return res;
}
public void back(int[] nums) {
if (path.size() >= nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (path.contains(nums[i])) {
continue;
}
path.add(nums[i]);
back(nums);
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,89 @@
package cn.whaifree.redo.redo_all_240721;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/3 13:15
* @注释
*/
public class LeetCode47 {
@Test
public void test()
{
int[] nums = new int[]{1, 1, 2};
List<List<Integer>> x = new Solution1().permuteUnique(nums);
for (List<Integer> integers : x) {
System.out.println(integers);
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
back(nums);
return res;
}
public void back(int[] nums) {
if (path.size() >= nums.length) {
res.add(path.stream().mapToInt(value -> nums[value]).boxed().toList());
return;
}
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (path.contains(i)) {
continue;
}
if (set.contains(nums[i])) {
continue;
}
set.add(nums[i]);
path.add(i);
back(nums);
path.remove(path.size() - 1);
}
}
}
class Solution1 {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = null;
public List<List<Integer>> permuteUnique(int[] nums) {
used = new boolean[nums.length];
Arrays.sort(nums);
back(nums);
return res;
}
public void back(int[] nums) {
if (path.size() >= nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i - 1] == nums[i] && !used[i - 1]) {
continue;
}
if (used[i]) { // 每条路径同一个元素不会再使用
continue;
}
used[i] = true;
path.add(nums[i]);
back(nums);
used[i] = false;
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,146 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/2 23:57
* @注释
*/
public class LeetCode491 {
public static void main(String[] args) {
new Solution().findSubsequences(new int[]{4, 6, 7, 7}).forEach(System.out::println);
// new Solution1().subsetsWithDup(new int[]{1,2,2}).forEach(System.out::println);
}
static class Solution {
List<List<Integer>> res;
List<Integer> path;
public List<List<Integer>> findSubsequences(int[] nums) {
res = new ArrayList<>();
path = new ArrayList<>();
back(0, nums);
return res;
}
/**
* 4677
* 4
* 6
* 7 7 这两个要去重在这层用set
*
*
*
* @param start
* @param nums
*/
public void back(int start, int[] nums) {
if (path.size() > 1) {
res.add(new ArrayList<>(path));
}
if (start >= nums.length) {
return;
}
HashSet<Integer> set = new HashSet<>();
for (int i = start; i < nums.length; i++) {
if (set.contains(nums[i])) {
continue;
}
if (path.isEmpty() || path.get(path.size() - 1) <= nums[i]) {
path.add(nums[i]);
set.add(nums[i]);
back(i + 1, nums);
path.remove(path.size() - 1);
}
}
}
}
// 错误used只能用在相邻的判断
// static class Solution2 {
// List<List<Integer>> res;
// List<Integer> path;
// boolean[] used = null;
// public List<List<Integer>> findSubsequences(int[] nums) {
// res = new ArrayList<>();
// path = new ArrayList<>();
// used = new boolean[nums.length];
// back(0, nums);
// return res;
// }
//
// /**
// * 4677
// * 4
// * 6
// * 7 7 这两个要去重在这层用set
// *
// *
// *
// * @param start
// * @param nums
// */
// public void back(int start, int[] nums) {
// if (path.size() > 1) {
// res.add(new ArrayList<>(path));
// }
// if (start >= nums.length) {
// return;
// }
// for (int i = start; i < nums.length; i++) {
// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
// continue;
// }
// if (path.isEmpty() || path.get(path.size() - 1) <= nums[i]) {
// path.add(nums[i]);
// used[i] = true;
// back(i + 1, nums);
// used[i] = false;
// path.remove(path.size() - 1);
// }
// }
// }
// }
static class Solution1 {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
boolean[] used = null;
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
used = new boolean[nums.length];
backTracking(nums, 0);
return res;
}
void backTracking(int[] nums, int start) {
res.add(new ArrayList<>(temp));
if (start >= nums.length) {
return;
}
for (int i = start; i < nums.length ; i++) {
// u1 u2 相同并且u1曾经被调用过则不继续进行本次循环
// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
// continue;
// }
temp.add(nums[i]);
used[i] = true;
backTracking(nums,i+1);
temp.remove(temp.size() - 1);
used[i] = false;
}
}
}
}

View File

@ -0,0 +1,85 @@
package cn.whaifree.redo.redo_all_240721;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/3 13:34
* @注释
*/
public class LeetCode51 {
@Test
public void test()
{
Solution solution = new Solution();
int n = 4;
System.out.println(solution.solveNQueens(n));
}
class Solution {
char[][] map;
List<List<String>> res = new ArrayList<>();
/**
*
* @param col 确定下来下次递归
* @param n
*/
public void back(int col, int n) {
if (col == n) {
List<String> path = new ArrayList<>();
for (char[] chars : map) {
String s = new String(chars);
path.add(s);
}
res.add(path);
return;
}
for (int i = 0; i < n; i++) {
if (isValid(col, i, n)) {
map[i][col] = 'Q';
back(col + 1, n);
map[i][col] = '.';
}
}
}
public List<List<String>> solveNQueens(int n) {
map = new char[n][n];
for (char[] chars : map) {
Arrays.fill(chars, '.');
}
back(0, n);
return res;
}
boolean isValid(int col, int row, int n) {
// 往左查找
for (int i = 0; i < col; i++) {
if (map[row][i] == 'Q') {
return false;
}
}
// 左上45
for (int i = col, j = row; i >= 0 && j >= 0; i--, j--) {
if (map[j][i] == 'Q') {
return false;
}
}
// 左下45
for (int i = col, j = row; i >= 0 && j < n; i--, j++) {
if (map[j][i] == 'Q') {
return false;
}
}
return true;
}
}
}