【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列
(一)题目
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.
(二)解题
主要思想:对于i,j这一点来说,它到终点的路径数dp[i][j] = dp[i+1][j]+ dp[i][j+1],这就是状态转移方程,然后利用动态规划来求解!
递归版本
class Solution {
public:
int dp[101][101];//用来标记已经计算过的路径
int uniquePaths(int m, int n) {
dp[m-1][n-1] = 1;
int ret = dfsPath(0,0,m-1,n-1);
return ret;
}
int dfsPath(int pm,int pn,int m ,int n)
{
if(pm==m && pn==n) return 1 ;
int down = 0;
int right = 0;
if(pm+1<=m) down = dp[pm+1][pn]==0?dfsPath(pm+1,pn,m,n):dp[pm+1][pn];//往下走的那一格到终点的路径数
if(pn+1<=n) right = dp[pm][pn+1]==0?dfsPath(pm,pn+1,m,n):dp[pm][pn+1];//往右走的那一格到终点的路径数
dp[pm][pn] = down+right;
return dp[pm][pn];
}
};
非递归版本
/*
提示:这个版本画个图可能会更好理解
*/
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[101][101];
for(int i = 0 ; i < m ; i++) dp[i][n-1] = 1;//首先初始化dp
for(int i = 0 ; i < n ; i++) dp[m-1][i] = 1;
if(m==1||n==1) return 1;//特殊情况
for(int i = m-2 ; i>=0 ; i--)
for(int j = n-2 ; j>=0 ; j--)
{
dp[i][j] = dp[i+1][j] + dp[i][j+1];//状态转移方程
}
return dp[0][0];
}
};
【一天一道LeetCode】#62. Unique Paths的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- LeetCode 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 ...
- [leetcode]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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- LeetCode 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- vue开发中v-for在Eslint的规则检查下出现:Elements in iteration expect to have 'v-bind:key' directives
在使用VScode编辑器vue开发过程中,v-for在Eslint的规则检查下出现报错:Elements in iteration expect to have 'v-bind:key' direct ...
- python3+django2 开发易语言网络验证(上)
创作背景: 在某论坛中下载到一套php开发易语言网络验证的教程,照着看下来,花了两天的时间,结果发现教程里开发的网络验证,以及随着教程一起给学员的源码,都存在着根本用不了的bug!我想要看看能不能在原 ...
- Java中使用long类型实现精确的四则运算
引子 Effective Java 2nd Edition 第48条建议:如果需要精确的答案,请避免使用float和doble.float和double类型主要是为了科学计算和工程计算而设计的.他们执 ...
- Android 实现串口的移植
安卓串口的实现,需要底层C++配合,不过这次我们根据framework中的思想,直接用API修改提供给JAVA层调用,这个就比较简单了. DEV项目需要,要实现在Android中实现串口的收发功能,有 ...
- Compile C++ code in Matlab with OpenCV support
Provides a function named as "mex_opencv(src)" The code function mex_opencv(src) ARC = 'x6 ...
- python 列表解析与map和filter函数
不知哪儿看到一个说法,大概是当map的函数参数可以直接引用一个已有的函数变量时(比如内建函数int,str之类的),用map更优美些,否则还是用列表解析更直观和快速. 我同意此说法. 昨天在写一个函数 ...
- ubuntu cpu频率控制
安装cpufrequtils: sudo apt-get install cpufrequtils 查看cpu: sudo cpufreq-info 设置cpu模式: ...
- 根据ccid取得账户,更改某段值再创建账户,返回新的ccid
CREATE OR REPLACE PACKAGE cux_cuxaprebate_utl IS * =============================================== * ...
- 剑指Offer——搜狐畅游笔试题+知识点总结
剑指Offer--搜狐畅游笔试题+知识点总结 情景回顾 时间:2016.9.24 10:00-12:00 地点:山东省网络环境智能计算技术重点实验室 事件:搜狐畅游笔试 注意事项:要有大局观,该舍 ...
- tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署
对于非Maven的web项目,有时候我们想不时常通过打war包.拷贝war包.启动tomcat来运行项目.这时候我们可以通过以下方式来进行配置: 1.1:创建web工程.工程结构如下: 1.2. ...