标签:
code ganker: http://codeganker.blogspot.com/2014/03/unique-paths-leetcode.html
public class Solution { public int uniquePaths(int m, int n) { if (m == 0 && n == 0){ return 0; } int[][] dp = new int[m][n]; for (int i = 0; i< m; i++) { dp[i][0] = 1; } for (int j = 0; j < n; j++) { dp[0][j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i][j -1] + dp[i-1][j]; } } return dp[m-1][n-1]; } }
典型的二维DP,dp[i][j]表示从左上角(0,0)到(i,j)的paths。状态转移方程:dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
参考:http://www.cnblogs.com/springfor/p/3886603.html
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4519221.html