feat(redo): 添加 LeetCode75、124 和300 的解决方案
- LeetCode75: 实现了颜色分类问题的解决方案 - LeetCode124: 提供了二叉树中的最大路径和问题的解答 - LeetCode300: 完成了最长递增子序列问题的实现
This commit is contained in:
parent
5c7eae4444
commit
ba8baf0b9d
@ -0,0 +1,50 @@
|
||||
package cn.whaifree.redo.redo_all_241121;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/11/23 12:32
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode124 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode treeNode = TreeNode.constructTree(new Integer[]{9, 9, 20, null, null, 15, 7});
|
||||
System.out.println(new Solution().maxPathSum(treeNode));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
int max = Integer.MIN_VALUE;
|
||||
/**
|
||||
* 每个节点的路径来源于 左边 右边 上面
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public int maxPathSum(TreeNode root) {
|
||||
int in = in(root);
|
||||
return max;
|
||||
}
|
||||
|
||||
public int in(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int left = in(root.left);
|
||||
if (left < 0) {
|
||||
left = 0;// 左边拖后腿,直接不采用
|
||||
}
|
||||
int right = in(root.right);
|
||||
if (right < 0) {
|
||||
right = 0;// 右边拖后腿,直接不采用
|
||||
}
|
||||
max = Math.max(max, left + right + root.val);
|
||||
return Math.max(left, right) + root.val;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package cn.whaifree.redo.redo_all_241121;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/11/21 22:50
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode300 {
|
||||
@Test
|
||||
public void test() {
|
||||
Solution solution = new Solution();
|
||||
int[] nums = {10,9,2,5,3,7,101,18};
|
||||
System.out.println(solution.lengthOfLIS(nums));
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
/**
|
||||
* 最长递增子序列
|
||||
* [10,9,2,5,3,7,101,18]
|
||||
* 1 1 1 2
|
||||
*
|
||||
* right从左到右
|
||||
* left 往左搜索到0,找到有序列更大的
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public int lengthOfLIS(int[] nums) {
|
||||
int[] dp = new int[nums.length];
|
||||
Arrays.fill(dp, 1);
|
||||
int max = 1;
|
||||
for (int right = 1; right < nums.length; right++) {
|
||||
int left = right - 1;
|
||||
while (left >= 0) {
|
||||
if (nums[left] < nums[right]) {
|
||||
dp[right] = Math.max(dp[left] + 1, dp[right]);
|
||||
}
|
||||
left--;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.whaifree.redo.redo_all_241121;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/11/21 22:44
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode75 {
|
||||
|
||||
class Solution {
|
||||
public void sortColors(int[] nums) {
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums, i, left);
|
||||
if (nums[i] == 1) { // 有可能index0是1,然后没有这一段就会被换到最后去哦
|
||||
swap(nums, i, right);
|
||||
}
|
||||
left++;
|
||||
right++;
|
||||
}else if (nums[i] == 1) {
|
||||
swap(nums, i, right);
|
||||
right++;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void swap(int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user