[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 ...
随机推荐
- Oracle SQL调优之表设计
在看<收获,不止sql优化>一书,并做了笔记,本博客介绍一下一些和调优相关的表比如分区表.临时表.索引组织表.簇表以及表压缩技术 分区表使用与查询频繁而更新数据不频繁的情况,不过要记得加全 ...
- 我是如何理解并使用maven的
前言 一直想写一篇关于Maven的文章,但是不知如何下笔,如果说能使用,会使用Maven的话,一.两个小时足矣,不需要搞懂各种概念.那么给大家来分享下我是如何理解并使用maven的. 什么是Maven ...
- SQL Server 2014:为什么数据库里的表提示“单元格是只读的”,不能修改?该如何处理?
出现以上这种情况,首先看一下这个字段的属性“标识规范”是不是选了“是”,自增属性下是不能修改的,属于只读.
- springboot项目POM文件第一行报错 Unknown Error
改成 war 不错了,但是打包麻烦 pom 文件报错 UnKnown Error第一次碰到这个问题,花了几个小时才解决,除了UnKnown 没有任何提示.网上搜的大部分情况都不是我遇到的. 还是没有解 ...
- java截取字符串中的最后几个字符
Java中的String类提供了一个substring(int from, int to)方法用于截取字符串中位置为from到to-1位置的字符. 因为字符串的字符位置是从0开始的,而substrin ...
- mysql批量更新数据(性能优化)--第二种方式
Spring+Mybatis 手动控制事务 参考: https://blog.csdn.net/qq_41750175/article/details/87621170 public boolean ...
- WPF 隐藏式控件
没用Popup用的面板控件,全部代码使用xaml的触发器. 代码: <Grid> <DockPanel> <StackPanel Background=" Do ...
- RESTful服务最佳实践(转)
原文:https://www.cnblogs.com/jaxu/p/7908111.html 译文作者:Jaxu 英文原文:https://files-cdn.cnblogs.com/files/ja ...
- ICSharpCode.SharpZipLib 中文乱码问题
今天在调用ICSharpCode.SharpZipLib解压zip文件时出现了中文文件乱码的问题. 解决过程如下: 1.判断是否压缩包本身问题.经查zip文件夹在本地直接解压打开时正确的中文名称,所以 ...
- 前端开发HTML5——基础标签
什么是HTML? HTML是HyperText Markup Language(超文本标记语言)的简写,他不是一种编程语言,而是一种标记语言,用于告诉浏览器如何构造你的页面.“超文本”就是指页面可以包 ...