该commit包含了对四个不同的LeetCode问题的解决方案。每个问题都是通过一个单独的Java类实现的,这些类被添加到cn.whaifree.redo.redo_all_240721包中。具体问题和解决方案如下:

1. [LeetCode40.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode40.java):解决组合总和II的问题,通过回溯算法找出所有不重复的组合。
2. [LeetCode90.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode90.java):解决子集II的问题,同样使用回溯算法,找出所有可能的不重复子集。
3. [LeetCode131.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode131.java):解决分割回文串的问题,提供两种解决方案:动态规划和回溯算法。
4. [LeetCode450.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode450.java):解决删除二叉搜索树中的节点的问题,实现了一种删除节点的方法。
5. [LeetCode501.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode501.java):找出二叉搜索树中的模式,通过遍历算法找出出现次数最多的元素。6. [LeetCode701.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode701.java):解决向二叉搜索树插入节点的问题,提供了一个递归的解决方案。

这些解决方案都是针对LeetCode上的特定问题,目的是提供清晰、高效的代码实现,以便其他开发者可以学习和参考。
This commit is contained in:
whaifree 2024-08-02 23:47:30 +08:00
parent 6320272406
commit 28a454f3ae
6 changed files with 348 additions and 0 deletions

View File

@ -0,0 +1,114 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/1 21:45
* @注释
*/
public class LeetCode131 {
public static void main(String[] args) {
new Solution1().partition("aab").forEach(System.out::println);
}
static class Solution {
/**
* 分割回文串
* aab
* <p>
* boolean[][] dp
* <p>
* dp[i][j]表示i~j是不是回文
* <p>
* if dp[i+1][j-1]==true && s[i] == s[j]
* dp[i][j] == true
* <p>
* i从大到小
* j从小到大
* <p>
* <p>
* 初始化
* i=j true
* <p>
* a a b
* <p>
* 0 1 2 3
* 0 1
* 1 1 1 0
* 2 1 0
* 3 1
*
* @param s
* @return
*/
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
int length = s.length();
boolean[][] dp = new boolean[length + 1][length + 1];
Set<String> o = new HashSet<>();
for (int i = length; i > 0; i--) {
for (int j = i; j <= length; j++) {
if (i == j) {
o.add(s.substring(i - 1, j));
dp[i][j] = true;
continue;
}
if ((i == j - 1 || dp[i + 1][j - 1]) && s.charAt(i - 1) == s.charAt(j - 1)) {
o.add(s.substring(i - 1, j));
dp[i][j] = true;
}
}
}
o.forEach(o1 -> System.out.println(o1));
// res.add(o);
return res;
}
}
static class Solution1 {
List<List<String>> res = new ArrayList<>();
List<String> path = new ArrayList<>();
public List<List<String>> partition(String s) {
back(s, 0);
return res;
}
public void back(String s, int start) {
if (start >= s.length()) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i < s.length(); i++) {
if (isHuiWen(s, start, i)) {
path.add(s.substring(start, i + 1));
back(s, i + 1);
path.remove(path.size() - 1);
}
}
}
public boolean isHuiWen(String s, int start, int end) {
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}
}

View File

@ -0,0 +1,56 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/29 21:53
* @注释
*/
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();
}
);
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
int nowSum = 0;
boolean[] used = null;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
used = new boolean[candidates.length];
back(candidates, target, 0);
return res;
}
public void back(int[] candidates, int target, int start) {
if (nowSum == target) {
res.add(new ArrayList<>(path));
return;
}
if (nowSum > target) {
return;
}
for (int i = start; i < candidates.length; i++) {
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
continue;
}
path.add(candidates[i]);
nowSum += candidates[i];
used[i] = true; // 这个被使用了
back(candidates, target, i + 1);
used[i] = false;
path.remove(path.size() - 1);
nowSum -= candidates[i];
}
}
}

View File

@ -0,0 +1,41 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 22:36
* @注释
*/
public class LeetCode450 {
public static void main(String[] args) {
// deleteNode(TreeNode.constructTreeByArray(5, 3, 6, 2, 4, null, 7), 3).printTree();
deleteNode(TreeNode.constructTreeByArray(50,30,70,null,40,60,80), 50).printTree();
}
public static TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return root;
}
if (root.val == key) {
TreeNode tmp = root.right;
if (tmp == null) {
return root.left;
}
TreeNode tmpLeft = root.left;
TreeNode index = tmp;
while (index.left != null) {
index = index.left;
}
index.left = tmpLeft;
root = tmp;
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else {
root.right = deleteNode(root.right, key);
}
return root;
}
}

View File

@ -0,0 +1,55 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/28 22:52
* @注释
*/
public class LeetCode501 {
public static void main(String[] args) {
TreeNode treeNode = TreeNode.constructTreeByArray(1,1,2,null,null,2);
int[] mode = new LeetCode501().findMode(treeNode);
for (int j : mode) {
System.out.println(j);
}
}
List<Integer> res = new ArrayList<>();
int max = 0;
int tmpV = Integer.MAX_VALUE;
int tmpCount = 1;
public int[] findMode(TreeNode root) {
find(root);
return res.stream().mapToInt(Integer::intValue).toArray();
}
public void find(TreeNode root) {
if (root == null) {
return;
}
find(root.left);
if (tmpV == root.val) {
tmpCount++;
}else {
tmpV = root.val;
tmpCount = 1;
}
if (tmpCount > max) {
max = tmpCount;
res.clear();
res.add(root.val);
} else if (tmpCount == max) {
res.add(root.val);
}
find(root.right);
}
}

View File

@ -0,0 +1,33 @@
package cn.whaifree.redo.redo_all_240721;
import cn.whaifree.leetCode.model.TreeNode;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/7/29 21:43
* @注释
*/
public class LeetCode701 {
public static void main(String[] args) {
new LeetCode701().insertIntoBST(TreeNode.constructTreeByArray(4, 2, 7, 1, 3), 5).printTree();
new LeetCode701().insertIntoBST(TreeNode.constructTreeByArray(40,20,60,10,30,50,70), 25).printTree();
}
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (root.val > val) {
root.left = insertIntoBST(root.left, val);
} else if (root.val < val) {
root.right = insertIntoBST(root.right, val);
}
return root;
}
}

View File

@ -0,0 +1,49 @@
package cn.whaifree.redo.redo_all_240721;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/1 21:33
* @注释
*/
public class LeetCode90 {
public static void main(String[] args) {
int[] nums = {1, 2, 2};
LeetCode90 leetCode90 = new LeetCode90();
System.out.println(leetCode90.subsetsWithDup(nums));
}
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
boolean[] used = null;
public List<List<Integer>> subsetsWithDup(int[] nums) {
used = new boolean[nums.length];
Arrays.sort(nums);
back(nums, 0);
return res;
}
public void back(int[] nums, int start) {
if (start > nums.length) {
return;
}
res.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
continue;
}
path.add(nums[i]);
used[i] = true;
back(nums, i + 1);
used[i] = false;
path.remove(path.size() - 1);
}
}
}