LeetCode 62.Unique Paths
文章目录
robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
大致题意
机器人每次只能向下向右走一步,给予一个mxn大小的方格,共有几种方法可以到达右下角(机器人在左上角)
题解
题目类型:动态规划
dp[i][j]表示到达第i行第j列有几种方法,初始为1。
状态转移:dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
下方为解
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> path(m, vector<int>(n, 1));
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
return path[m - 1][n - 1];
}
};

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论