diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java index 29cb0f7..8650adc 100644 --- a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java @@ -15,15 +15,14 @@ public class LeetCode17 { @Test public void test() { - String digits = ""; - new Solution().letterCombinations(digits).forEach(s -> System.out.println(s)); + String digits = "23"; + new Solution1().letterCombinations(digits).forEach(s -> System.out.println(s)); } class Solution { List map = new ArrayList<>(); - List result = new ArrayList<>(); - + // 记录 路径 StringBuilder s = new StringBuilder(); public List letterCombinations(String digits) { @@ -59,6 +58,7 @@ public class LeetCode17 { 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); @@ -67,4 +67,53 @@ public class LeetCode17 { } } } + + class Solution1 { + List map = new ArrayList<>(); + List result = new ArrayList<>(); + // 记录 路径 + StringBuilder s = new StringBuilder(); + + public List 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); + } + } + } } diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode39.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode39.java new file mode 100644 index 0000000..ba469d2 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode39.java @@ -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> res = new ArrayList<>(); + List path = new ArrayList<>(); + public List> 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); + } + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode40.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode40.java new file mode 100644 index 0000000..a040d47 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode40.java @@ -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> res = new LinkedList<>(); + List 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> 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,表示还没从递归中出来,即是【1,1】中的第二个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); + } + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode110.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode110.java new file mode 100644 index 0000000..37c1157 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode110.java @@ -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; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode203.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode203.java new file mode 100644 index 0000000..c5ae95b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode203.java @@ -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; + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode206.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode206.java new file mode 100644 index 0000000..58cab4f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode206.java @@ -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); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode216.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode216.java new file mode 100644 index 0000000..18ecbcf --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode216.java @@ -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 integers : new Solution().combinationSum3(9, 45)) { + System.out.println(integers); + } + } + + + + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + /** + * 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件: + * @param k + * @param n + * @return + */ + public List> 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; + } + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode222.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode222.java new file mode 100644 index 0000000..080650a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode222.java @@ -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; + } + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode236.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode236.java new file mode 100644 index 0000000..e92187c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode236.java @@ -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; + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode450.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode450.java new file mode 100644 index 0000000..7af27fd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode450.java @@ -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; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode501.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode501.java new file mode 100644 index 0000000..a4f5248 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode501.java @@ -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 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); + } + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode701.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode701.java new file mode 100644 index 0000000..5efe0c0 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode701.java @@ -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; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode77.java b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode77.java new file mode 100644 index 0000000..c525d1f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_4/LeetCode77.java @@ -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 integers : new Solution().combine(5, 2)) { + System.out.println(integers); + } + } + + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + + /** + * 返回范围 [1, n] 中所有可能的 k 个数的组合。 + * @param n + * @param k + * @return + */ + public List> 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