Last active
July 5, 2019 07:42
-
-
Save yanjinbin/c7a122ff3529491691dd958bf8a3a55d to your computer and use it in GitHub Desktop.
最小路径和 递归求法
This file contains hidden or 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
http://bit.ly/2RVNkuQ | |
我哭了 | |
``` | |
class Solution { | |
// 64. 最小路径和 TL | |
public int minPathSum(int[][] grid) { | |
int rowLen = grid.length; | |
int colLen = grid[rowLen-1].length; | |
int[][] memo = new int[rowLen][colLen]; | |
return routerHelper(rowLen - 1, colLen - 1, grid, memo); | |
} | |
public int routerHelper(int i, int j, int[][] grid, int[][] memo) { | |
if (memo[i][j] > 0) { | |
return memo[i][j]; | |
} | |
if (i == 0 && j == 0) { | |
return memo[i][j] = grid[0][0]; | |
} | |
if (i == 0) { | |
return memo[i][j] = routerHelper(i, j - 1, grid, memo) + grid[i][j]; | |
} | |
if (j == 0) { | |
return memo[i][j] = routerHelper(i - 1, j, grid, memo) + grid[i][j]; | |
} | |
return memo[i][j] = Math.min(routerHelper(i - 1, j, grid, memo), routerHelper(i, j - 1, grid, memo)) + grid[i][j]; | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment