From ba8baf0b9d6b3007edeab474b10ff7e29c532454 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 24 Nov 2024 11:06:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(redo):=20=E6=B7=BB=E5=8A=A0=20LeetCode75?= =?UTF-8?q?=E3=80=81124=20=E5=92=8C300=20=E7=9A=84=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LeetCode75: 实现了颜色分类问题的解决方案 - LeetCode124: 提供了二叉树中的最大路径和问题的解答 - LeetCode300: 完成了最长递增子序列问题的实现 --- .../redo/redo_all_241121/LeetCode124.java | 50 +++++++++++++++++++ .../redo/redo_all_241121/LeetCode300.java | 49 ++++++++++++++++++ .../redo/redo_all_241121/LeetCode75.java | 35 +++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode124.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode300.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode75.java diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode124.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode124.java new file mode 100644 index 0000000..12855e3 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode124.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redo_all_241121; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/23 12:32 + * @注释 + */ +public class LeetCode124 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{9, 9, 20, null, null, 15, 7}); + System.out.println(new Solution().maxPathSum(treeNode)); + } + + class Solution { + int max = Integer.MIN_VALUE; + /** + * 每个节点的路径来源于 左边 右边 上面 + * @param root + * @return + */ + public int maxPathSum(TreeNode root) { + int in = in(root); + return max; + } + + public int in(TreeNode root) { + if (root == null) { + return 0; + } + + int left = in(root.left); + if (left < 0) { + left = 0;// 左边拖后腿,直接不采用 + } + int right = in(root.right); + if (right < 0) { + right = 0;// 右边拖后腿,直接不采用 + } + max = Math.max(max, left + right + root.val); + return Math.max(left, right) + root.val; + } + + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode300.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode300.java new file mode 100644 index 0000000..939bd5a --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode300.java @@ -0,0 +1,49 @@ +package cn.whaifree.redo.redo_all_241121; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/21 22:50 + * @注释 + */ +public class LeetCode300 { + @Test + public void test() { + Solution solution = new Solution(); + int[] nums = {10,9,2,5,3,7,101,18}; + System.out.println(solution.lengthOfLIS(nums)); + } + + + class Solution { + /** + * 最长递增子序列 + * [10,9,2,5,3,7,101,18] + * 1 1 1 2 + * + * right从左到右 + * left 往左搜索到0,找到有序列更大的 + * @param nums + * @return + */ + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + int max = 1; + for (int right = 1; right < nums.length; right++) { + int left = right - 1; + while (left >= 0) { + if (nums[left] < nums[right]) { + dp[right] = Math.max(dp[left] + 1, dp[right]); + } + left--; + } + } + return max; + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode75.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode75.java new file mode 100644 index 0000000..fc3d988 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241121/LeetCode75.java @@ -0,0 +1,35 @@ +package cn.whaifree.redo.redo_all_241121; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/11/21 22:44 + * @注释 + */ +public class LeetCode75 { + + class Solution { + public void sortColors(int[] nums) { + int left = 0; + int right = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] == 0) { + swap(nums, i, left); + if (nums[i] == 1) { // 有可能index0是1,然后没有这一段就会被换到最后去哦 + swap(nums, i, right); + } + left++; + right++; + }else if (nums[i] == 1) { + swap(nums, i, right); + right++; + } + } + } + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + } +}