redo
This commit is contained in:
whai 2024-02-07 01:03:35 +08:00
parent 3c3f373663
commit 4fe744c27a
13 changed files with 718 additions and 4 deletions

View File

@ -15,15 +15,14 @@ public class LeetCode17 {
@Test @Test
public void test() { public void test() {
String digits = ""; String digits = "23";
new Solution().letterCombinations(digits).forEach(s -> System.out.println(s)); new Solution1().letterCombinations(digits).forEach(s -> System.out.println(s));
} }
class Solution { class Solution {
List<String> map = new ArrayList<>(); List<String> map = new ArrayList<>();
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
// 记录 路径
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
public List<String> letterCombinations(String digits) { public List<String> letterCombinations(String digits) {
@ -59,6 +58,7 @@ public class LeetCode17 {
int c = digits.charAt(number) - 48; int c = digits.charAt(number) - 48;
String sValue = map.get(c); String sValue = map.get(c);
int length = sValue.length(); int length = sValue.length();
// 对每个字符处理
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char c1 = sValue.charAt(i); char c1 = sValue.charAt(i);
s.append(c1); s.append(c1);
@ -67,4 +67,53 @@ public class LeetCode17 {
} }
} }
} }
class Solution1 {
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);
return result;
}
/**
*
* @param digits 原始字符 23
* @param number 使用到第几个字符
*/
void backTracking(String digits, int number) {
if (s.length() == digits.length()) {
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);
s.deleteCharAt(s.length() - 1);
}
}
}
} }

View File

@ -0,0 +1,51 @@
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/6 23:57
* @注释
*/
public class LeetCode39 {
@Test
public void test() {
new Solution().combinationSum(new int[]{2, 3, 6, 7}, 7).forEach(
list -> {
list.forEach(System.out::print);
System.out.println();
}
);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
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));
}
for (int i = index; i < candidates.length ; i++) {
path.add(candidates[i]);
backTracking(candidates, i, need - candidates[i]);
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,76 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/6 23:57
* @注释
*/
public class LeetCode40 {
@Test
public void test() {
new Solution().combinationSum2(new int[]{10,1,2,7,6,1,5}, 8).forEach(
list -> {
list.forEach(System.out::print);
System.out.println();
}
);
}
class Solution {
List<List<Integer>> res = new LinkedList<>();
List<Integer> path = new ArrayList<>();
boolean[] used;
/**
* 结果不能重复
* 1. 排序candidates
* 2. 使用used[]数组记录使用
* 3. 一旦candi[index]==cnadi[index-1]&&user[index]=true那么就是用过的不添加
* @param candidates
* @param target
* @return
*/
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
used = new boolean[candidates.length];
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));
}
for (int i = index; i < candidates.length ; i++) {
// 取第一个数先 used放1再放0
// 取第二个数时如果can两个数相同并且used为0表示前一个数用过
// 如果 used为1表示还没从递归中出来即是11中的第二个1是需要的
if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
continue;
}
path.add(candidates[i]);
used[i] = true;
backTracking(candidates, i+1, need - candidates[i]);
used[i] = false;
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,47 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/3 19:17
* @注释
*/
public class LeetCode110 {
@Test
public void test() {
System.out.println(new Solution().isBalanced(TreeNode.constructTreeByArray(1,null,2,null,3)));
}
class Solution {
public boolean isBalanced(TreeNode root) {
return balance(root) != -1;
}
public int balance(TreeNode root) {
if (root == null) {
return 0;
}
int right = balance(root.right);
if (right == -1) {
return -1;
}
int left = balance(root.left);
if (left == -1) {
return -1;
}
// 左右两边高度差
if (Math.abs(right - left) > 1) {
return -1;
}
// 返回高度
return Math.max(right, left)+ 1;
}
}
}

View File

@ -0,0 +1,34 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/6 23:05
* @注释
*/
public class LeetCode203 {
@Test
public void test() {
new Solution().removeElements(ListNode.listNodeFromArray(new int[]{1, 2, 3, 4}), 4).printList();
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return null;
}
head.next = removeElements(head.next, val);
if (head.val == val) {
return head.next;
}else {
return head;
}
}
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/6 23:18
* @注释
*/
public class LeetCode206 {
@Test
public void test() {
new Solution().reverseList(ListNode.listNodeFromArray(new int[]{1, 2, 3})).printList();
}
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
return reverse(null, head);
}
public ListNode reverse(ListNode pre, ListNode after) {
if (after == null) {
return pre;
}
ListNode afterNext = after.next;
after.next = pre;
return reverse(after, afterNext);
}
}
}

View File

@ -0,0 +1,62 @@
package cn.whaifree.redo.redo_24_2_4;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/4 13:07
* @注释
*/
public class LeetCode216 {
@Test
public void test() {
for (List<Integer> integers : new Solution().combinationSum3(9, 45)) {
System.out.println(integers);
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
/**
* 找出所有相加之和为 n k 个数的组合且满足下列条件
* @param k
* @param n
* @return
*/
public List<List<Integer>> combinationSum3(int k, int n) {
recursion(1, 9, k, n);
return res;
}
void recursion(int start, int end, int k, int needNumber) {
if (needNumber == 0 && path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= end; i++) {
// 标准剪枝
if (end - i + 1 < k - path.size()) {
return;
}
path.add(i);
recursion(i + 1, end, k, needNumber - i);
path.remove(path.size() - 1);
// 如果 新加的数i 需要的数needNumber大那么加这些正数是多余的操作
if (needNumber <= i) {
return;
}
}
}
}
}

View File

@ -0,0 +1,52 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/3 20:04
* @注释
*/
public class LeetCode222 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5, 6});
treeNode.printTree();
System.out.println(new Solution().countNodes(treeNode));
}
class Solution{
/**
* 完全二叉树二分查找
* @param root
* @return
*/
public int countNodes(TreeNode root) {
if (root==null) return 0;
TreeNode left = root.left;
int leftDepth = 0;
while (left != null) {
leftDepth++;
left = left.left;
}
TreeNode right = root.right;
int rightDepth = 0;
while (right != null) {
rightDepth++;
right = right.right;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
}

View File

@ -0,0 +1,44 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/4 0:08
* @注释
*/
public class LeetCode236 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6);
new Solution().lowestCommonAncestor(treeNode, treeNode.right, treeNode.left).printTree();
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
// 左右两边都不为空那么我就是最近公共节点
return root;
}
if (left != null) {
return left;
}else {
return right;
}
}
}
}

View File

@ -0,0 +1,50 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/4 0:26
* @注释
*/
public class LeetCode450 {
@Test
public void test() {
new Solution().deleteNode(TreeNode.constructTreeByArray(5,3,6,2,4,null,7), 7).printTree();
}
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root==null) return null;
if (root.val == key) {
// 该节点要删除
if (root.right == null) {
return root.left;
}
if (root.left == null) {
return root.right;
}
TreeNode pre = root.right;
while (pre.left != null) {
pre = pre.left;
}
pre.left = root.left;
return root.right;
}
if (root.val > key) {
root.left = deleteNode(root.left, key);
} else {
root.right = deleteNode(root.right, key);
}
return root;
}
}
}

View File

@ -0,0 +1,73 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/3 23:28
* @注释
*/
public class LeetCode501 {
@Test
public void test() {
TreeNode root = TreeNode.constructTreeByArray(6,2,8,0,4,7,9,null,null,2,6);
for (int i : new Solution().findMode(root)) {
System.out.println(i);
}
}
class Solution {
TreeNode pre = null;
int most = 0;
int markCount = 0;
List<Integer> res = new ArrayList<>();
public int[] findMode(TreeNode root) {
recursion(root);
int[] re = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
re[i] = res.get(i);
}
return re;
}
public void recursion(TreeNode root) {
if (root == null) {
return;
}
recursion(root.left);
if (pre == null) {
// 第一个节点走
markCount = 1;
} else if (pre.val == root.val) {
markCount++;
} else {
markCount = 1;
}
if (markCount > most) {
res.clear();
most = markCount;
res.add(root.val);
} else if (markCount == most) {
res.add(root.val);
}
pre = root;
recursion(root.right);
}
}
}

View File

@ -0,0 +1,65 @@
package cn.whaifree.redo.redo_24_2_4;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/4 12:18
* @注释
*/
public class LeetCode701 {
@Test
public void test() {
new Solution1().insertIntoBST(TreeNode.constructTreeByArray(4, 2, 7, 1, 3), 6).printTree();
}
class Solution {
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;
}
}
class Solution1 {
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 {
root.right = insertIntoBST(root.right, val);
}
return root;
}
}
}

View File

@ -0,0 +1,72 @@
package cn.whaifree.redo.redo_24_2_4;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/4 12:31
* @注释
*/
public class LeetCode77 {
@Test
public void test(
) {
for (List<Integer> integers : new Solution().combine(5, 2)) {
System.out.println(integers);
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
/**
* 返回范围 [1, n] 中所有可能的 k 个数的组合
* @param n
* @param k
* @return
*/
public List<List<Integer>> combine(int n, int k) {
recursion(1, n, k);
return res;
}
/**
* 从start end 里选择一个数放入path
* @param start 开始的数
* @param end 结束的数
* @param k
*/
void recursion(int start, int end, int k) {
if (path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= end; i++) {
// 剪枝
// 已经选择的数字size
// 还需选择的数k-size
// 可以选择的数 end-start
// 如果end-start<k-size 直接return
if (end - i + 1 < k - path.size()) {
return;
}
path.add(i);
recursion(i + 1, end, k);
path.remove(path.size() - 1);
}
}
}
}