leet-code/src/main/java/leetcode/editor/cn/UniquePaths.java
huangge1199@hotmail.com a957bc7a6b 62:不同路径
2021-08-09 23:26:21 +08:00

84 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
//
// 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
//
// 问总共有多少条不同的路径?
//
//
//
// 示例 1
//
//
//输入m = 3, n = 7
//输出28
//
// 示例 2
//
//
//输入m = 3, n = 2
//输出3
//解释:
//从左上角开始,总共有 3 条路径可以到达右下角。
//1. 向右 -> 向下 -> 向下
//2. 向下 -> 向下 -> 向右
//3. 向下 -> 向右 -> 向下
//
//
// 示例 3
//
//
//输入m = 7, n = 3
//输出28
//
//
// 示例 4
//
//
//输入m = 3, n = 3
//输出6
//
//
//
// 提示:
//
//
// 1 <= m, n <= 100
// 题目数据保证答案小于等于 2 * 109
//
// Related Topics 数学 动态规划 组合数学
// 👍 1065 👎 0
package leetcode.editor.cn;
/**
* @author huangge1199
*/
//62:不同路径
public class UniquePaths {
public static void main(String[] args) {
//测试代码
Solution solution = new UniquePaths().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int uniquePaths(int m, int n) {
int[][] f = new int[m][n];
for (int i = 0; i < m; ++i) {
f[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
f[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
return f[m - 1][n - 1];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}