【一天一道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 ...
随机推荐
- HTTP请求分析工具Fiddler
主要用于分析http头信息和响应头信息,以及具体的post数据和响应数据,可以监测电脑上http请求.
- python类(class)中参数self的解释说明
python类(class)中参数self的简单解释 1.self只有在类的方法中才会有,其他函数或方法是不必带self的. 2.在调用时不必传入相应的参数.3.在类的方法中(如__init__),第 ...
- 用go实现常用算法与数据结构——队列(queue)
queue 简介 队列是一种非常常见的数据结构,日常生活中也能经常看到.一个典型的队列如下图(图片来自 segmentfault): 可以看出队列和我们日常生活中排队是基本一致的.都遵循 FIFO(F ...
- Java中的内存分配
Java程序在运行时,需要在内存中分配空间,为了提高效率,就对空间进行了不同区域的划分,因为每一片区域否有特定的处理数据方式和内存管理方式. 1.栈存储局部变量 2.堆存储new出来的东西 3.方法区 ...
- PTA中如何出Java题目?
PTA中如何出Java题目? 很多第一次出题的老师,不知道Java在PTA中是如何处理输入的.写一篇文章供大家参考.比如以下这样的一个题目: 从控制台读入两个数,然后将其相加输出. 对于该题可以有如下 ...
- 在查询语句中使用 NOLOCK 和 READPAST
对于非银行等严格要求事务的行业,搜索记录中出现或者不出现某条记录,都是在可容忍范围内,所以碰到死锁,应该首先考虑,我们业务逻辑是否能容忍出现或者不出现某些记录,而不是寻求对双方都加锁条件下如何解锁的问 ...
- ListView常见的优化方式简述
ListView的优化 对于ListView来说,应该算是布局中几种最常用的组件之一了,使用也十分方便,下面个大家介绍一下两种常见的优化方式. 1.条目复用优化 其实listview的工作原理就是,l ...
- 5秒让你的View变3D,ThreeDLayout使用和实现
在很久很久以前,写了一篇自定义3d view的博客.但是只是讲了如何实现,实现起来还是比较耗时,所以本着平易近人的心态,把他封装成了一个ViewGroup,只需要在你的view或者布局外面包裹一层Th ...
- 项目部署、配置、查错常用到的Linux命令
一.常用命令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) ll 会列出该文件下的所有文件信息,包括隐藏的文件的文件详细信息, ...
- OpenResty修改Nginx默认autoindex页面
Nginx的autoindex 命令可以自动列出目录下的文件,一些网站用这个功能做文件下载,但是Nginx又没有提供这个页面的 自定义的功能,后来看到别人提及 ngx_openresty,才想到 bo ...