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