题目地址
题目描述
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.
解题思路
一看到这题就想到无脑递归,但是最后看了看,觉得很有可能会 TEL ,所以放弃选择了 DP 。我们可以简单推出 Start (DP[0][0]) 到 Finish 的路径个数等于他的下边方格(DP[1][0])和右边方格(DP[0][1])之和。继续往下推也是这个道理,所以最后有递推公式:
dp[i][j] = dp[i+1][j] + dp[i][j+1];
解题代码【.CPP】
1 | class Solution { |