版权声明:版权所有,转载请注明原网址链接。 https://blog.csdn.net/qq_41231926/article/details/82834260
我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/unique-paths/description/
题目描述:
知识点:动态规划
思路:动态规划
状态定义:f(x, y) ---------- 到达坐标(x, y)的路径数
状态转移:
(1)如果x == 0或者y == 0,f(x, y) = 1。
(2)否则,f(x, y) = f(x - 1, y) + f(x, y - 1)。
时间复杂度和空间复杂度均是O(m * n)。
JAVA代码:
public class Solution {
public int uniquePaths(int m, int n) {
int[][] map = new int[m][n];
for (int i = 0; i < n; i++) {
map[0][i] = 1;
}
for (int i = 0; i < m; i++) {
map[i][0] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
map[i][j] = map[i - 1][j] + map[i][j - 1];
}
}
return map[m - 1][n - 1];
}
}
LeetCode解题报告: