[leetcode]第11周作业
来源:互联网 发布:java导出csv文件乱码 编辑:程序博客网 时间:2024/05/03 21:25
题目: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?
分析:
可以类比那道Climbing Stairs 爬梯子问题,而这道题是每次可以向下走或者向右走,求到达最右下角的所有不同走法的个数。那么跟爬梯子问题一样,我们需要用动态规划Dynamic Programming来解。
我们可以维护一个二维数组dp,其中dp[i][j]表示到当前位置不同的走法的个数,然后可以得到递推式为:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
代码:
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
- [leetcode]第11周作业
- [leetcode]第12周作业
- [leetcode]第13周作业
- [leetcode]第14周作业
- [leetcode]第15周作业
- [leetcode] 第16周作业
- [leetcode] 第一周作业
- [leetcode] 第四周作业
- Leetcode ArrayNesting(第一周作业)
- 数据库:第11周作业
- 第11周作业1(LeetCode31)
- 第11周作业2(LeetCode16)
- 第11堂作业
- 第11次作业
- 第11堂作业
- 第11次作业
- 第11课作业
- 第5周作业
- [Unity][Mesh][NavMesh]RuntimeNavMeshBuilder: Source mesh does not allow read解决办法
- Flume架构以及应用介绍
- 按F12才能进入开机界面(戴尔游匣7559)
- 团队天梯赛L1-030. 一帮一
- PHP基本语法2
- [leetcode]第11周作业
- 摩尔定律还有效吗?
- 安装多版本 cuda ,多版本之间切换
- [译] Ubuntu 命令行安装 Eclipse [umake]
- VMware 家族系列各组件介绍
- java中加号为连接字符与运算符的区别
- 使用Sqoop从MySQL导入数据到Hive和HBase 及近期感悟
- 模板实现顺序表、双链表
- 【Java】常用控件和事件处理