leetcode题解更新:452、738、763

添加了新的题解文件:
- [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`方法,通过记录每个字符最后出现的位置进行划分。

这些题解展示了多种算法技巧,包括排序、贪心、字符串处理和数组操作,涉及不同的数据结构和问题解决策略。欢迎参考和讨论。
```
This commit is contained in:
whaifree 2024-08-09 00:01:24 +08:00
parent 8b775ad439
commit 3aaee55251
3 changed files with 175 additions and 0 deletions

View File

@ -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<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] < o2[1] ? -1 : 1;
// if (o1[1]<o2[1])return -1;
// return 1;
}
});
/**
* | |
* | |
* | |
* | |
*/
int end = points[0][1];
int arrows = 1;
for (int i = 1; i < points.length; i++) {
int nextStart = points[i][0];
if (end < nextStart) {
end = points[i][1];
arrows++;
}
}
return arrows;
}
}

View File

@ -0,0 +1,63 @@
package cn.whaifree.redo.redo_all_240721;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/8 22:21
* @注释
*/
public class LeetCode738 {
@Test
public void test()
{
Solution solution = new Solution();
System.out.println(solution.monotoneIncreasingDigits(13211432));
System.out.println(solution.monotoneIncreasingDigits(299999));
System.out.println(solution.monotoneIncreasingDigits(555555));
System.out.println(solution.monotoneIncreasingDigits(23456));
System.out.println(solution.monotoneIncreasingDigits(1254321));
System.out.println(solution.monotoneIncreasingDigits(332));
System.out.println(Integer.parseInt("0999"));
}
class Solution {
/**
* 13211432
* 09999999
*
* 5432
* 4999
* @param n
* @return
*/
public int monotoneIncreasingDigits(int n) {
String s = String.valueOf(n);
byte[] bytes = s.getBytes();
int start = Integer.MAX_VALUE;
for (int i = bytes.length - 1; i > 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));
}
}
}

View File

@ -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<Integer> 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<Integer> 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;
}
}
}