[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 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 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3
Output: 28
这道题让求所有不同的路径的个数,一开始还真把博主难住了,因为之前好像没有遇到过这类的问题,所以感觉好像有种无从下手的感觉。在网上找攻略之后才恍然大悟,原来这跟之前那道 Climbing Stairs 很类似,那道题是说可以每次能爬一格或两格,问到达顶部的所有不同爬法的个数。而这道题是每次可以向下走或者向右走,求到达最右下角的所有不同走法的个数。那么跟爬梯子问题一样,需要用动态规划 Dynamic Programming 来解,可以维护一个二维数组 dp,其中 dp[i][j] 表示到当前位置不同的走法的个数,然后可以得到状态转移方程为: dp[i][j] = dp[i - 1][j] + dp[i][j - 1],这里为了节省空间,使用一维数组 dp,一行一行的刷新也可以,代码如下:
解法一:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> dp(n, );
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
dp[j] += dp[j - ];
}
}
return dp[n - ];
}
};
这道题其实还有另一种很数学的解法,参见网友 Code Ganker 的博客,实际相当于机器人总共走了 m + n - 2步,其中 m - 1 步向右走,n - 1 步向下走,那么总共不同的方法个数就相当于在步数里面 m - 1 和 n - 1 中较小的那个数的取法,实际上是一道组合数的问题,写出代码如下:
解法二:
class Solution {
public:
int uniquePaths(int m, int n) {
double num = , denom = ;
int small = m > n ? n : m;
for (int i = ; i <= small - ; ++i) {
num *= m + n - - i;
denom *= i;
}
return (int)(num / denom);
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/62
类似题目:
参考资料:
https://leetcode.com/problems/unique-paths/
https://leetcode.com/problems/unique-paths/discuss/22981/My-AC-solution-using-formula
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 62. Unique Paths 不同的路径的更多相关文章
- 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] 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 、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不同路径 (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 不同路径
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(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- [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 ...
随机推荐
- python在字节流中对int24的转换
python在字节流中对int24的转换 概述 最近在写项目的过程中,需要对从串口中读取的数据进行处理,本来用C写完了,但是却一直拿不到正确的数据包,可能是因为自己太菜了.后来用了python重新写了 ...
- 扩展centos7.4虚拟机磁盘大小
虚拟机分配磁盘40GB,实际系统分区只用了20GB,需要手工扩展到40GB,操作方法如下: 查看磁盘信息(确认主分区只有17GB):[root@test-web1 ~]# df -hFilesyste ...
- vue-cli安装以及创建一个简单的项目(二)(vuex使用、发行一个简单的app)
1.vuex的使用 vuex是vue的状态管理中心,vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,常用于: 1.多个视图依赖同一状态(l例:菜单导航) 2.来自不同 ...
- QT+OpenGL(02)-- zlib库的编译
1.zlib库的下载 http://www.zlib.net/ zlib1211.zip 2.解压 3.进入 zlib1211\zlib-1.2.11\contrib\vstudio\vc14 目录 ...
- vue 上传进度显示
参考资料: https://ask.csdn.net/questions/767017 https://www.cnblogs.com/best-fyx/p/11363506.html 我使用的是el ...
- C# Random
一.简介 在Random类用于创建随机数.(当然是伪随机的.) 二.Random用法 例: Random rnd = new Random(); int month = rnd.Next(1, 13) ...
- AJAX小示例
一. 基本内容 定义:AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步的Javascript和XML",即使用Javascript语言 ...
- 关于css中的定位
关于前端的几种定位方式 近期自己感觉自己对于前端定位的知识还是不是太理解,所以自己就在这里做一个总结 1.元素的定位属性主要包括定位模式和边偏移两部分. 边偏移属性 描述 top bott ...
- 家电制造业中MES系统发挥的作用
人们对MES系统在家电生产领域的应用并不陌生,这是离散型MES应用的一个重要领域. 如空调.冰箱.电视.洗衣机等生产加工中均具有批量制造.多品种小批量.单件生产的等特点,很多企业也是以以订单.合同为核 ...
- Android 主题、样式
样式是针对View的,比如TextView.Button等控件,主题是针对Activity.整个APP的. 样式.主题是多种属性的集合,类似于网页中的CSS样式,可以让设计与内容分离,并且可以继承.复 ...