leetcode62-Unique Paths
来源:互联网 发布:更改本机mac地址 编辑:程序博客网 时间:2024/06/05 20:07
问题描述:
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方格的左上角,每次只能向下走或者向右走,要到达右下角,有多少种走法?
问题求解:
path[i][j]表示要到达(i,j)处所有可能的步数。要到达(i,j)处有两种可能:
(1)从(i-1,j)处向下走一步。(2)从(i,j-1)处向右走一步。
得到状态转移方程:
path[i][j] = path[i-1][j]+path[i][j-1];
并且:path[i][0] = 1;path[0][j] = 1;
代码如下:
class Solution {public: int uniquePaths(int m, int n) { int path[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(i==0 || j==0) path[i][j]=1;//无论目标点再第一行或第一列都只有一种可能 else {//path[i][j]有2种可能:1、(i-1,j)处向下走。2、(i,j-1)处向右走 path[i][j] = path[i-1][j]+path[i][j-1]; } } } return path[m-1][n-1]; }};
0 0
- LeetCode62:Unique Paths
- Leetcode62 Unique Paths
- leetcode62-Unique Paths
- leetcode62.[DP] Unique Paths
- Leetcode62: Unique Paths
- LeetCode62. Unique Paths
- leetcode62. Unique Paths
- leetcode62. Unique Paths
- leetcode62. Unique Paths
- Leetcode62-63 Unique Paths
- LeetCode62——Unique Paths
- 【LeetCode62 Unique Paths】动态规划计算路径
- leetcode62&63&64_Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Oracle:ORA-28056: Writing audit records to Windows Event Log failed
- STL模板库在ACM中的应用(I)
- Hadoop中的一些基本操作
- 关于JAVA堆栈的认识【转】
- 数据库语句
- leetcode62-Unique Paths
- Codeforces 617B Chocolate (Round #340 (Div. 2) B题)
- JavaScript 数组(Array) sort 排序函数实现的简单高阶函数原理
- PHP遍历文件夹内文件和子文件
- 实现log4j日志文件在相对路径保存
- JavaScript优化(二)
- ubuntu输入正确用户密码重新跳到无法登录
- Android 中随机画线
- [工具](1)ItelliJ IDEA使用日记