From fd440507c4de55b7b8ec13cd395bcf85bfe54f55 Mon Sep 17 00:00:00 2001 From: whaifree Date: Sun, 6 Oct 2024 12:42:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=94=E4=B8=AALeetCode?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E9=A2=98=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=20-=20=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode12=EF=BC=88?= =?UTF-8?q?=E6=95=B4=E6=95=B0=E8=BD=AC=E7=BD=97=E9=A9=AC=E6=95=B0=E5=AD=97?= =?UTF-8?q?=EF=BC=89=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=20-=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode13=EF=BC=88=E7=BD=97?= =?UTF-8?q?=E9=A9=AC=E6=95=B0=E5=AD=97=E8=BD=AC=E6=95=B4=E6=95=B0=EF=BC=89?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88=20-=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86LeetCode57=EF=BC=88=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E5=8C=BA=E9=97=B4=EF=BC=89=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=20-=20=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode92=EF=BC=88?= =?UTF-8?q?=E5=8F=8D=E8=BD=AC=E9=93=BE=E8=A1=A8=E7=9A=84=E4=B8=80=E9=83=A8?= =?UTF-8?q?=E5=88=86=EF=BC=89=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= =?UTF-8?q?=20-=20=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode124=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E6=9C=80=E5=A4=A7=E8=B7=AF=E5=BE=84=E5=92=8C?= =?UTF-8?q?=EF=BC=89=E7=9A=84=E8=A7=A3=E5=86=B3=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redo/redo_all_240924/LeetCode12.java | 40 +++++++++++++ .../redo/redo_all_240924/LeetCode124.java | 33 +++++++++++ .../redo/redo_all_240924/LeetCode13.java | 51 ++++++++++++++++ .../redo/redo_all_240924/LeetCode57.java | 51 ++++++++++++++++ .../redo/redo_all_240924/LeetCode92.java | 59 +++++++++++++++++++ 5 files changed, 234 insertions(+) create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode12.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode124.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode13.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode57.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode92.java diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode12.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode12.java new file mode 100644 index 0000000..8eca53c --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode12.java @@ -0,0 +1,40 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 11:12 + * @注释 + */ +public class LeetCode12 { + @Test + public void test(){ + Solution solution = new Solution(); + String s = solution.intToRoman(1994); + System.out.println(s); + } + + + class Solution{ + // 每个标准-前一个 9 5 4 1 + int[] nums = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; + String[] romans = new String[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; + public String intToRoman(int num) { + StringBuilder sb = new StringBuilder(); + while (num > 0) { + for (int i = 0; i < nums.length; i++) { + if (num >= nums[i]) { + sb.append(romans[i]); + num -= nums[i]; + break; + } + } + if (num == 0) break; + } + return sb.toString(); + } + + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode124.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode124.java new file mode 100644 index 0000000..47d65a4 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode124.java @@ -0,0 +1,33 @@ +package cn.whaifree.redo.redo_all_240924; + +import cn.whaifree.leetCode.model.TreeNode; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 12:22 + * @注释 + */ +public class LeetCode124 { + class Solution { + + int max = Integer.MIN_VALUE; + public int maxPathSum(TreeNode root) { + if (root == null) { + return 0; + } + int left = maxPathSum(root.left); + if (left < 0) { + // 不采纳左边的 + left = 0; + } + int right = maxPathSum(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_240924/LeetCode13.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode13.java new file mode 100644 index 0000000..bce6a0b --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode13.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 11:32 + * @注释 + */ +public class LeetCode13 { + + @Test + public void test() { + Solution solution = new Solution(); + System.out.println(solution.romanToInt("MCMXCIV")); + } + + class Solution { + static Map map; + static { + map = new HashMap<>(); + map.put('I', 1); + map.put('V', 5); + map.put('X', 10); + map.put('L', 50); + map.put('C', 100); + map.put('D', 500); + map.put('M', 1000); + } + public int romanToInt(String s) { + char[] charArray = s.toCharArray(); + + int res = 0; + for (int i = s.length() - 1; i >= 0; i--) { + char c = charArray[i]; + Integer v = map.get(c); + if (i > 0 && v > map.get(charArray[i - 1])) { + v -= map.get(charArray[i - 1]); + i--; + } + res += v; + } + return res; + } + } + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode57.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode57.java new file mode 100644 index 0000000..f938a88 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode57.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_all_240924; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 11:49 + * @注释 + */ +public class LeetCode57 { + + class Solution { + /** + * | | | | | | + * | | + * + * @param intervals + * @param newInterval + * @return + */ + public int[][] insert(int[][] intervals, int[] newInterval) { + int[][] res = new int[intervals.length + 1][2]; + int i = 0; + int index = 0; + // 找到newInterval的左边界位置 + while (intervals[index][0] < newInterval[0]) { + res[i++] = intervals[index]; + index++; + } + // 此时index的value >= newInterval的起始位置 + + // 找到newInterval右边边界位置 + while (intervals[index][1] <= newInterval[1]) { + newInterval[0] = Math.min(newInterval[0], intervals[index][0]); + newInterval[1] = Math.max(newInterval[1], intervals[index][1]); + index ++; + } + + res[i++] = newInterval; + // 把newInterval右边界右边的位置全部移动过来 + while (index < intervals.length) { + res[i++] = intervals[index]; + index++; + } + return Arrays.copyOf(res, i); + } + } + + +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode92.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode92.java new file mode 100644 index 0000000..d8c2ce4 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode92.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_all_240924; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/6 12:07 + * @注释 + */ +public class LeetCode92 { + + @Test + public void test() { + ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5}); + + ListNode listNode1 = new Solution().reverseBetween(listNode, 1, 5); + listNode1.printList(); + + } + + class Solution { + public ListNode reverseBetween(ListNode head, int left, int right) { + ListNode pre = new ListNode(-1, head); + ListNode index = pre; + for (int i = 0; i < left - 1; i++) { + index = index.next; + } + + ListNode revHead = index.next; + ListNode indexB = pre; + for (int i = 0; i < right; i++) { + indexB = indexB.next; + } + ListNode revTail = indexB; + ListNode after = indexB.next; + // 逆转revHead到revTail + + ListNode reverse = reverse(null, revHead, right - left + 1); + index.next = reverse; + revHead.next = after; + return pre.next; + } + + public ListNode reverse(ListNode pre, ListNode after,int k) { + if (after == null) { + return pre; + } + if (k == 0) { + return pre; + } + ListNode tmp = after.next; + after.next = pre; + return reverse(after, tmp, k - 1); + } + + } +}