LeetCode62 Unique Paths
2016-09-13 22:21
295 查看
题目:
A 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? (Medium)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
分析:
今天时间比较晚了,先写两道比较熟悉的题目吧。
典型的矩阵型动态规划,dp[i][j]定义为到当前节点的...(视题目而定)。
所以本题dp[i][j]表示到当前节点的不同路径数。则第一行全为1,第一列全为1。
后续的 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
A 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? (Medium)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
分析:
今天时间比较晚了,先写两道比较熟悉的题目吧。
典型的矩阵型动态规划,dp[i][j]定义为到当前节点的...(视题目而定)。
所以本题dp[i][j]表示到当前节点的不同路径数。则第一行全为1,第一列全为1。
后续的 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
class Solution { public: int uniquePaths(int m, int n) { int dp[m] = {0}; dp[0][0] = 1; for (int i = 0; i < n; ++i) { dp[0][i] = 1; } for (int i = 0; i < m; ++i) { dp[i][0] = 1; } for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1]; } };
相关文章推荐
- LeetCode 62 Unique Paths
- leetcode-62 Unique Paths
- leetcode—— 62——Unique Paths
- LeetCode-62-Unique Paths
- LeetCode(62): Unique Paths
- [leetcode 62] Unique Paths
- LeetCode62——Unique Paths
- Leetcode 62 Unique Paths
- leetcode || 62、Unique Paths
- leetcode_question_62 Unique Paths
- Leetcode62: Unique Paths
- LeetCode-62-Unique Paths(C语言实现)
- [LeetCode 62] Unique Paths(教科书般的动态规划)
- leetcode_question_62 Unique Paths
- LeetCode 62 Unique Paths
- LeetCode_62---Unique Paths
- Leetcode62-63 Unique Paths
- Leetcode 62: Unique Paths
- leetcode62 Unique Paths
- LeetCode 62 Unique Paths