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); + } + + } +}