leecode更新
This commit is contained in:
parent
e9ab1f5244
commit
33c16119c6
|
|
@ -41,10 +41,10 @@ public class T23_42_Trap {
|
|||
|
||||
if (height[left] < height[right]) {
|
||||
//左边一定的积水
|
||||
sum += Math.min(leftMax, rightMax) - height[left];
|
||||
sum += leftMax - height[left];
|
||||
left++;
|
||||
} else {
|
||||
sum += Math.min(leftMax, rightMax) - height[right];
|
||||
sum += rightMax - height[right];
|
||||
right--;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-04-26 10:25
|
||||
*@Description:
|
||||
* TODO 力扣46 全排列:
|
||||
* 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T24_46_Permute {
|
||||
|
||||
List<List<Integer>> result = new ArrayList<>();
|
||||
List<Integer> cur = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* 重点需要做树枝去重
|
||||
* @param nums
|
||||
* @return
|
||||
*/
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
backtracking(nums, new boolean[nums.length]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void backtracking(int[] nums, boolean[] used) {
|
||||
if (nums.length == cur.size()) {
|
||||
result.add(new ArrayList<>(cur));
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (used[i]) continue;
|
||||
if (i > 0 && nums[i] == nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
used[i] = true;
|
||||
cur.add(nums[i]);
|
||||
backtracking(nums, used);
|
||||
cur.remove(cur.size() - 1);
|
||||
used[i] = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-04-26 10:39
|
||||
*@Description:
|
||||
* TODO 力扣48 旋转图像:
|
||||
* 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
|
||||
* 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T25_48_Rotate {
|
||||
|
||||
|
||||
/**
|
||||
* 官方题解中的一种方法:
|
||||
* 先水平反转,在对角线先反转
|
||||
* @param matrix
|
||||
*/
|
||||
public void rotate(int[][] matrix) {
|
||||
|
||||
//水平反转
|
||||
for (int i = 0; i < matrix.length / 2; i++) {
|
||||
for (int j = 0; j < matrix[0].length; j++) {
|
||||
int temp = matrix[i][j];
|
||||
matrix[i][j] = matrix[matrix.length - 1 - i][j];
|
||||
matrix[matrix.length - 1 - i][j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
//对角线反转
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
int temp = matrix[i][j];
|
||||
matrix[i][j] = matrix[j][i];
|
||||
matrix[j][i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -58,4 +58,18 @@ public class T27_53_MaxSubArray {
|
|||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int maxSubArray2(int[] nums) {
|
||||
|
||||
int dp0=Integer.MIN_VALUE;
|
||||
int dp1=nums[0];
|
||||
|
||||
for(int i=1;i<nums.length;i++){
|
||||
dp0 = Math.max(dp0,dp1);
|
||||
dp1 = Math.max(dp1+nums[i],nums[i]);
|
||||
}
|
||||
|
||||
return Math.max(dp0,dp1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-04-26 10:55
|
||||
*@Description:
|
||||
* TODO 力扣55 跳跃游戏:
|
||||
* 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
|
||||
* 数组中的每个元素代表你在该位置可以跳跃的最大长度。
|
||||
* 判断你是否能够到达最后一个下标。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T28_55_CanJump {
|
||||
|
||||
public boolean canJump(int[] nums) {
|
||||
if (nums.length <= 1) return true;
|
||||
int maxIndex = nums[0];
|
||||
|
||||
for (int i = 1; i < nums.length - 1; i++) {
|
||||
if (i > maxIndex) return false;
|
||||
maxIndex = Math.max(maxIndex, i + nums[i]);
|
||||
if (maxIndex >= nums.length - 1) return true;
|
||||
}
|
||||
return maxIndex >= nums.length - 1;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -53,4 +53,27 @@ public class T29_56_Merge {
|
|||
return result.toArray(new int[0][]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int[][] merge1(int[][] intervals) {
|
||||
Arrays.sort(intervals, ((o1, o2) -> {
|
||||
return o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0];
|
||||
}));
|
||||
ArrayList<int[]> result = new ArrayList<>();
|
||||
result.add(intervals[0]);
|
||||
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (intervals[i][0] <= result.get(result.size() - 1)[1]) {
|
||||
int[] pre = result.get(result.size() - 1);
|
||||
pre[1] = Math.max(intervals[i][1], pre[1]);
|
||||
} else {
|
||||
result.add(intervals[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result.toArray(new int[0][]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-04-26 11:26
|
||||
*@Description:
|
||||
* TODO 力扣62 不同路径:
|
||||
* 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
|
||||
* 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
|
||||
* 问总共有多少条不同的路径?
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T30_62_UniquePaths {
|
||||
|
||||
public int uniquePaths(int m, int n) {
|
||||
|
||||
int[][] dp = new int[m][n];
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
dp[i][0] = 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
dp[0][i] = 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n; j++) {
|
||||
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return dp[m - 1][n - 1];
|
||||
|
||||
}
|
||||
|
||||
|
||||
//滚动数组优化
|
||||
public int uniquePaths1(int m, int n) {
|
||||
|
||||
int[] dp = new int[n];
|
||||
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
dp[i] = 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < m; i++) {
|
||||
for (int j = 1; j < n ; j++) {
|
||||
dp[j] = dp[j] + dp[j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return dp[n - 1];
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
package com.markilue.leecode.hot100.second;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100.second
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-04-26 11:35
|
||||
*@Description:
|
||||
* TODO 力扣64 最小路径和:
|
||||
* 给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
|
||||
* 说明:每次只能向下或者向右移动一步。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T31_64_MinPathSum {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
// int[][] grid = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
|
||||
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
|
||||
System.out.println(minPathSum(grid));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int minPathSum(int[][] grid) {
|
||||
|
||||
int[][] dp = new int[grid.length][grid[0].length];
|
||||
|
||||
dp[0][0] = grid[0][0];
|
||||
|
||||
for (int i = 1; i < grid.length; i++) {
|
||||
dp[i][0] = dp[i - 1][0] + grid[i][0];
|
||||
}
|
||||
|
||||
for (int i = 1; i < grid[0].length; i++) {
|
||||
dp[0][i] = dp[0][i - 1] + grid[0][i];
|
||||
}
|
||||
|
||||
for (int i = 1; i < grid.length; i++) {
|
||||
for (int j = 1; j < grid[0].length; j++) {
|
||||
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return dp[grid.length - 1][grid[0].length - 1];
|
||||
}
|
||||
|
||||
|
||||
int[][] memo;
|
||||
|
||||
//回溯+记忆化搜索
|
||||
public int minPathSum1(int[][] grid) {
|
||||
memo = new int[grid.length][grid[0].length];
|
||||
for (int i = 0; i < memo.length; i++) {
|
||||
Arrays.fill(memo[i], -1);
|
||||
}
|
||||
|
||||
return backtracking(grid, grid.length - 1, grid[0].length-1);
|
||||
}
|
||||
|
||||
public int backtracking(int[][] grid, int i, int j) {
|
||||
if (i < 0 || j < 0) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
}
|
||||
if (memo[i][j] != -1) {
|
||||
return memo[i][j];
|
||||
} else {
|
||||
memo[i][j] = Math.min(backtracking(grid, i - 1, j), backtracking(grid, i, j - 1)) + grid[i][j];
|
||||
}
|
||||
|
||||
return memo[i][j];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue