62.Unique Paths
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?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
一个机器人到右下角摘星星,只能向右向下移动,问有多少条路可达。
一个dp题,每一个格子可从他的上边或者左边走过来,那么到达这个格子的路径数 = 到达他上边格子的路径数 + 到达他左边格子的路径数。时间复杂度O(m*n),空间复杂度O(m*n)
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > ans(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i)
ans[i][0] = 1;
for (int i = 0; i < n; ++i)