Skip to content

Commit

Permalink
2023/5/30
Browse files Browse the repository at this point in the history
  • Loading branch information
KidJoe1412 committed May 30, 2023
1 parent 59c72dc commit 469968e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/leetcode/Q64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package leetcode;
/**
* 题干:最小路径和
* 给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
* 说明:每次只能向下或者向右移动一步。
* ******************************************************
* 解法:
* ******************************************************
* 原题:<a href="https://leetcode.cn/problems/minimum-path-sum/description/"></a>
* ******************************************************
*/
class Q64 {
public int minPathSum(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (i == 0 && j == 0) continue;
else if(i == 0) grid[i][j] = grid[i][j - 1] + grid[i][j];
else if(j == 0) grid[i][j] = grid[i - 1][j] + grid[i][j];
else grid[i][j] = Math.min(grid[i - 1][j],grid[i][j - 1]) + grid[i][j];
}
}
return grid[grid.length - 1][grid[0].length - 1];
}
}

0 comments on commit 469968e

Please sign in to comment.