[LeetCode]62 不同的路径总数
来源:互联网 发布:base64 js 源码 编辑:程序博客网 时间:2024/05/27 12:22
Unique Paths(不同的路径总数)
【难度:Medium】
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?
位于m*n矩阵的一个机器人只能向下或向右移动,求其从Start移动到Finish不同的走法总数。
解题思路
这是一道典型的动态规划问题,使用一个二维数组ans记忆到达每一点可行的走法总数。首先将左边界点和上边界点初始化为1,因为机器人起始与(0,0),左边界点和上边界点的走法只有1种。接下来的每一点(x,y),可以由(x-1,y)向右走或是(x,y-1)向下走来到达,因此在(x,y)这一点可到达的方法有ans[x-1][y]+ans[x][y-1]种,到达终点的方法则是ans最后一个点的数据。
c++代码如下
class Solution {public: int uniquePaths(int m, int n) { if (m == 0 || n == 0) { return 0; } vector<vector<int>> path(m,vector<int>(n,0)); for (int i = 0; i < m; i++) path[i][0] = 1; for (int i = 0; i < n; i++) path[0][i] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { path[i][j] = path[i-1][j] + path[i][j-1]; } } return path[m-1][n-1]; }};
0 0
- [LeetCode]62 不同的路径总数
- [LeetCode]63 不同的路径总数之二
- 1142 Relations dp n个数的不同大小关系总数
- SPOJ694 && SPOJ705 ——不同子串的总数
- oracle 查询同一字段不同状态下面的总数
- Oracle统计某一字段在不同值下的总数
- 不同的路径
- 题目 :不同的路径
- LintCode -- 不同的路径
- LintCode 不同的路径
- 不同的路径
- 不同的路径 II
- LintCode_不同的路径
- 不同的路径
- 不同的路径
- LintCode:不同的路径
- 不同的路径
- 不同的路径 II
- 认识数据仓库建设意义
- c语言:求三个数中的最大值(双分支结构)
- JavaScript 垃圾回收
- 【Manthan, Codefest 16B】【二分 or 递推暴力】A Trivial Problem 输出阶乘为m的所有数
- 微信-弹幕是怎么搞出来的,看svg!
- [LeetCode]62 不同的路径总数
- SVD奇异值分解
- Android内存优化-泛谈 (一)
- NYOJ 又见拦截导弹
- Eclipse is running in a JRE, but a JDK is required 解决方法
- Eclipse下搭建C/C++开发环境教程
- LZW Data Compression
- linux下查找包含关键字的文件
- 使用librtmp进行H264与AAC直播