This commit is contained in:
whai 2024-02-02 21:03:16 +08:00
parent d0f6cb7c3a
commit 3c3f373663
8 changed files with 598 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/2 10:31
* @注释
*/
public class LeetCode17 {
@Test
public void test() {
String digits = "";
new Solution().letterCombinations(digits).forEach(s -> System.out.println(s));
}
class Solution {
List<String> map = new ArrayList<>();
List<String> result = new ArrayList<>();
StringBuilder s = new StringBuilder();
public List<String> letterCombinations(String digits) {
if (digits.length() == 0) {
return result;
}
map.add(0, null);
map.add(1, null);
map.add(2, "abc");
map.add(3, "def");
map.add(4, "ghi");
map.add(5, "jkl");
map.add(6, "mno");
map.add(7, "pqrs");
map.add(8, "tuv");
map.add(9, "wxyz");
backTracking(digits, 0, digits.length());
return result;
}
/**
*
* @param digits 原始字符 23
* @param number 使用到第几个字符
* @param needLength 需要几个字符等价于digits.length
*/
void backTracking(String digits, int number,int needLength) {
if (s.length() == needLength) {
result.add(new String(s.toString()));
return;
}
int c = digits.charAt(number) - 48;
String sValue = map.get(c);
int length = sValue.length();
for (int i = 0; i < length; i++) {
char c1 = sValue.charAt(i);
s.append(c1);
backTracking(digits, number + 1, needLength);
s.deleteCharAt(s.length() - 1);
}
}
}
}

View File

@ -0,0 +1,81 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/2 8:22
* @注释
*/
public class LeetCode216 {
@Test
public void test() {
Solution solution = new Solution();
solution.combinationSum3(9, 45).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
List<List<Integer>> res = new LinkedList<>();
List<Integer> path = new ArrayList<>();
int sum = 0;
/**
* 相加之和为n的k个数的组合
* @param k
* @param n
* @return
*/
public List<List<Integer>> combinationSum3(int k, int n) {
circle(1, 9, n, k);
return res;
}
public void circle(int start, int end, int n, int k) {
if (path.size() == k && sum == n) {
res.add(new ArrayList<>(path));
return;
}
// 1. 如果sum>n了证明往后加只会更大因为都是正数就不再继续了
// sum>n
//
// 2. 如果 9个数要9个数
// 已经选择size
// 还需选择k-size
// 可以选择的数end-start
// 可以选择的数<还需选择的数 end-start<k-size
for (int i = start; i <= end ; i++) {
// 可以选择的数<还需选择的数
if (end - i + 1 < k - path.size()) {
return;
}
path.add(i);
sum += i;
// 如果 加上某个数已经超过了n那么剩下的正数都没必要再加了如1234后面只能选5678如果去了4加了5678必然会超过n
if (sum > n) {
path.remove(path.size() - 1);
sum -= i;
return;
}
circle(i + 1, end, n, k);
path.remove(path.size() - 1);
sum -= i;
}
}
}
}

View File

@ -0,0 +1,57 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 20:24
* @注释
*/
public class LeetCode77 {
@Test
public void test() {
new Solution().combine(4, 4).forEach(
list -> {
list.forEach(System.out::print);
System.out.println();
}
);
}
class Solution {
List<List<Integer>> res = new LinkedList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
circle(1, n, k);
return res;
}
void circle(int start, int end, int k) {
if (path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
// 剪枝可以选择的个数不满足最小需要的个数
// 已经选择的个数 path.size
// 还需要的个数 k-path.size
// 可以选择的个数<还需要的个数 end-i<k-path.size 退出
// end-i>k-path.size时正常执行 i<end-(k-path.size) 时正常执行
for (int i = start; i <= end - (k - path.size()) + 1; i++) {
path.add(i);
circle(i + 1, end, k);
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,40 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 18:56
* @注释
*/
public class LeetCode108 {
@Test
public void test() {
new Solution().sortedArrayToBST(new int[]{1, 2, 3, 4, 5, 6, 7, 8}).printTree();
}
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return construct(nums, 0, nums.length - 1);
}
public TreeNode construct(int[] nums, int start, int end) {
if (start > end) {
return null;
}
int middle = (end + start) / 2;
int num = nums[middle];
TreeNode root = new TreeNode(num);
root.left = construct(nums, start, middle - 1);
root.right = construct(nums, middle + 1, end);
return root;
}
}
}

View File

@ -0,0 +1,157 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 9:38
* @注释
*/
public class LeetCode450 {
@Test
public void test() {
new Solution1().deleteNode(TreeNode.constructTreeByArray(5,3,6,2,4,null,7,null,null), 0).printTree();
}
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null && root.val == key) {
return null;
}
TreeNode pre = null;
TreeNode tmp = root;
while (tmp!=null) {
if (tmp.val > key) {
pre = tmp;
tmp = tmp.left;
} else if (tmp.val < key) {
pre = tmp;
tmp = tmp.right;
} else {
break;
}
}
// 找不到该要删除的节点
if (pre != null && pre.val != key && tmp == null) {
return root;
}
// 左右都为空
// 左为空
// 右为空
if (pre.left == tmp) {
if (tmp.right == null && tmp.left == null) {
pre.left = null;
} else if (tmp.left == null) {
pre.left = tmp.right;
} else if (tmp.right == null) {
pre.left = tmp.left;
}
}else if (pre.right == tmp){
if (tmp.right == null && tmp.left == null) {
pre.right = null;
} else if (tmp.left == null) {
pre.right = tmp.right;
} else if (tmp.right == null) {
pre.right = tmp.left;
}
}
// 左右都不空
// tmp.left移动到tmp.right.left.....最后
TreeNode move = tmp.right;
while (move.left != null) {
move = move.left;
}
move.left = tmp.left;
// tmp.right.left移动到tmp的位置 有可能在pre的左边或右边
if (pre.left == tmp) {
pre.left = tmp.right;
} else {
pre.right = tmp.right;
}
return root;
}
}
class Solution1 {
/**
* 递归
* @param root
* @param key
* @return
*/
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) {
return root;
}
if (root.val == key) {
// 左边为空 返回右边
// 两边都为空则返回空随便返回子节点
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
}
TreeNode index = root.right;
while (index.left != null) {
index = index.left;
}
index.left = root.left;
return root.right;
}
if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
}
return root;
}
}
/**
* 把二叉树变成链表
*/
class Solution2 {
TreeNode pre = new TreeNode();
TreeNode cur = pre;
public TreeNode deleteNode(TreeNode root, int key) {
search(root, key);
return pre.right;
}
private void search(TreeNode root, int key) {
if (root == null) {
return;
}
search(root.left, key);
TreeNode r = root.right;
if (root.val != key) {
cur.right = root;
root.left = null;
root.right = null;
cur = cur.right;
}
search(r, key);
}
}
}

View File

@ -0,0 +1,65 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.Deque;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 19:15
* @注释
*/
public class LeetCode538 {
@Test
public void test() {
new Solution1().convertBST(TreeNode.constructTreeByArray(4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8)).printTree();
}
class Solution {
int sum = 0;
public TreeNode convertBST(TreeNode root) {
if (root == null) {
return null;
}
root.right = convertBST(root.right);
root.val += sum;
sum = root.val;
root.left = convertBST(root.left);
return root;
}
}
class Solution1 {
public TreeNode convertBST(TreeNode root) {
if (root == null) {
return null;
}
Deque<TreeNode> stack = new java.util.LinkedList<>();
stack.push(root);
int sum = 0;
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
if (pop == null) {
TreeNode ans = stack.pop();
ans.val += sum;
sum = ans.val;
} else {
if (pop.left!=null) stack.push(pop.left);
stack.push(pop);
stack.push(null);
if (pop.right!=null) stack.push(pop.right);
}
}
return root;
}
}
}

View File

@ -0,0 +1,40 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 14:01
* @注释
*/
public class LeetCode669 {
@Test
public void test() {
new Solution().trimBST(TreeNode.constructTreeByArray(3,0,4,null,2,null,null,1), 1, 3).printTree();
}
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if (root == null) {
return null;
}
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
if (root.val < low) {
return root.right;
}
if (root.val > high) {
return root.left;
}
return root;
}
}
}

View File

@ -0,0 +1,88 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/31 9:13
* @注释
*/
public class LeetCode701 {
@Test
public void test() {
new Solution2().insertIntoBST(TreeNode.constructTreeByArray(5,null,14,10,77,null,null,null,95,null,null), 4).printTree();
}
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
insert(root, val);
return root;
}
public void insert(TreeNode root, int val) {
if (root == null) {
return;
}
if (root.val < val) {
insert(root.right, val);
if (root.right == null) {
root.right = new TreeNode(val);
}
} else {
insert(root.left, val);
if (root.left == null) {
root.left = new TreeNode(val);
}
}
}
}
class Solution1 {
public TreeNode insertIntoBST(TreeNode root, int val) {
// 找到了叶子节点该位置就是要插入的地方
if (root == null) {
return new TreeNode(val);
}
if (root.val < val) {
root.right = insertIntoBST(root.right, val);
}else if (root.val > val){
root.left = insertIntoBST(root.left, val);
}
return root;
}
}
class Solution2 {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root==null) return new TreeNode(val);
TreeNode pre = null;
TreeNode index = root;
while (index != null) {
pre = index;
if (index.val > val) {
index = index.left;
} else {
index = index.right;
}
}
if (pre.val > val) {
pre.left = new TreeNode(val);
} else {
pre.right = new TreeNode(val);
}
return root;
}
}
}