[LeetCode] [C++] 62. Unique Paths
来源:互联网 发布:matlab cell数组 拼接 编辑:程序博客网 时间:2024/06/05 04:57
There are numbers in each grid in the picture,
which stand for the number of unique paths from Start to the grid.
Observing the picture,
we can easily find that,
for any i,j>0,
dp[i][j]=dp[i-1][j]+dp[i][j-1].
So I declared a 2D int array "dp" (Here I use vector<vector<int>>) to store the number of each grid.
At first, I put "1" into each grid of the first row and the first column,
Because along them, there is only 1 path.
Then I start looping from dp[1][1] to dp[m-1][n-1].
At last, the answer of the question is in the dp[m-1][n-1]
This is "Dynamic Programming".
The Runtime of the program is 0 ms.
The length of the code is 266 Bytes.
class Solution {public: int uniquePaths(int m, int n) { vector<vector<int>> dp(m,vector<int>(n,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]; }};
阅读全文
0 0
- 【C++】【LeetCode】62. Unique Paths
- [LeetCode] [C++] 62. Unique Paths
- [leetcode]Unique Paths(C)
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- [Leetcode] 62. Unique Paths
- [leetcode] 62.Unique Paths
- [leetcode] 62.Unique Paths
- leetcode 62. Unique Paths
- Leetcode 62. Unique Paths
- [leetcode] 62. Unique Paths
- 62. Unique Paths LeetCode
- [LeetCode]62. Unique Paths
- 【LeetCOde】62. Unique Paths
- [LeetCode]62. Unique Paths
- leetcode 62. Unique Paths
- LeetCode 62. Unique Paths
- 《大型分布式网站架构设计与实践》
- [51Nod1676 无向图同构]无向图哈希
- 成为Android高级工程师看这里就够了
- 随手记-处理图片内存溢出
- vue 刷新当前路由
- [LeetCode] [C++] 62. Unique Paths
- ActiveMQ之集群(主从)搭建-yellowcong
- TCP/IP五层模型消息解/封装仿真
- 营养餐 阶梯博弈
- Dagger2的简单使用
- springMVC中两种validation的简单使用
- 如何支付宝微信购买比特币?
- 处理excel
- Linux安装Java jdk