From 3aaee552519829673748eb626f9e5638129da36b Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Fri, 9 Aug 2024 00:01:24 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=E9=A2=98=E8=A7=A3=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=EF=BC=9A452=E3=80=81738=E3=80=81763?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了新的题解文件: - [LeetCode452.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode452.java):实现了`findMinArrowShots`方法,通过排序和贪心算法求解。- [LeetCode738.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode738.java):实现了`monotoneIncreasingDigits`方法,对数字进行特殊处理。 - [LeetCode763.java](file://src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode763.java):实现了`partitionLabels`方法,通过记录每个字符最后出现的位置进行划分。 这些题解展示了多种算法技巧,包括排序、贪心、字符串处理和数组操作,涉及不同的数据结构和问题解决策略。欢迎参考和讨论。 ``` --- .../redo/redo_all_240721/LeetCode452.java | 56 +++++++++++++++++ .../redo/redo_all_240721/LeetCode738.java | 63 +++++++++++++++++++ .../redo/redo_all_240721/LeetCode763.java | 56 +++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode452.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode738.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode763.java diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode452.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode452.java new file mode 100644 index 0000000..0287fa4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode452.java @@ -0,0 +1,56 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/8 21:46 + * @注释 + */ +public class LeetCode452 { + + @Test + public void test() + { + // [[10,16],[2,8],[1,6],[7,12]] + int[][] points = new int[][]{{10,16},{2,8},{1,6},{7,12}}; +// System.out.println(findMinArrowShots(points)); + // [[-2147483646,-2147483645],[2147483646,2147483647]] + int[][] points1 = new int[][]{{-2147483646,-2147483645},{2147483646,2147483647}}; + System.out.println(findMinArrowShots(points1)); + System.out.println(-2147483645-2147483647); + } + + public int findMinArrowShots(int[][] points) + { + Arrays.sort(points, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o1[1] < o2[1] ? -1 : 1; +// if (o1[1] 0; i--) { + byte after = bytes[i]; + byte before = bytes[i - 1]; + if (after < before) { + bytes[i - 1] -= 1; + start = i; + } + } + + if (start == Integer.MAX_VALUE) { + return n; + } + + for (int i = start; i < bytes.length; i++) { + bytes[i] = '9'; + } + + return Integer.parseInt(new String(bytes)); + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode763.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode763.java new file mode 100644 index 0000000..8743efe --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode763.java @@ -0,0 +1,56 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/8/8 22:07 + * @注释 + */ +public class LeetCode763 { + + @Test + public void test() + { + String s = "ababcbacadefegdehijhklij"; + System.out.println(new Solution().partitionLabels(s)); + } + + class Solution { + /** + * 获取每个byte最后出现的地方 + * + * 挨个遍历,直到索引的地方不是最后出现的地方,就截取。 + * 每次需要找到当前比遍历中最远到哪了 + * + * @param s + * @return + */ + public List partitionLabels(String s) { + byte[] bytes = s.getBytes(); + int[] index = new int[26]; + for (int i = 0; i < bytes.length; i++) { + index[bytes[i] - 97] = i; + } + + List res = new ArrayList<>(); + int start = 0; + int rightMax = 0; + for (int i = 0; i < bytes.length; i++) { + byte aByte = bytes[i]; + int ind = index[aByte - 97]; + rightMax = Math.max(rightMax, ind); // 当前最右边界 + if (ind == i && i == rightMax) { + res.add(rightMax - start + 1); + start = rightMax + 1; + } + } + + return res; + } + } +}