新增五个LeetCode算法题的解决方案

- 添加了LeetCode12(整数转罗马数字)的解决方案
- 添加了LeetCode13(罗马数字转整数)的解决方案
- 添加了LeetCode57(插入区间)的解决方案
- 添加了LeetCode92(反转链表的一部分)的解决方案
- 添加了LeetCode124(二叉树最大路径和)的解决方案
This commit is contained in:
whaifree 2024-10-06 12:42:33 +08:00
parent b3c66df7da
commit fd440507c4
5 changed files with 234 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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; // 一个节点只能选择一条路左路或者右路
}
}
}

View File

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

View File

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

View File

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