-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59c72dc
commit 469968e
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |