[LeetCode] 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] Unique Paths 不同的路径的更多相关文章
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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每天一题】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] 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 OJ: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] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
随机推荐
- Entity Framework入门系列(1)-扯淡开篇
这是我在Cnblogs上的第一个系列,但愿能坚持下去: 惯例索引 Entity Framework入门系列(1)-开篇兼索引: Entity Framework入门系列(2)-初试Code First ...
- JS性能优化
1.不要在同一行声明多个变量. 2.请使用 ===/!==来比较true/false或者数值 3.使用对象字面量替代new Array这种形式 4.不要使用全局函数. 5.Switch语句必须带有de ...
- C# async/await 使用总结
今天搞这两个关键字搞得有点晕,主要还是没有彻底理解其中的原理. 混淆了一个调用异步方法的概念: 在调用异步方法时,虽然方法返回一个 Task,但是其中的代码已经开始执行.该方法在调用时,即刻执行了一部 ...
- Win10 UWP开发系列:实现Master/Detail布局
在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...
- 学习EF之贪婪加载和延迟加载(1)
从暑假开始接触code first以来,一直感觉很好用,主要在于开发过程中以业务为中心可以随时修改数据模型生成数据库,还有一个原因就是查询起来很方便 这里找了一个以前database first的一段 ...
- 【夯实PHP基础】php开发时遇到白页的调试方法
本文地址 分享提纲: 1. 设置报错报错级别,显示报错 2. 白页的可能原因 1.[设置报错报错级别,显示报错] php开发时,访问地址也对,但就是不出来页面,显示的是 白的页面,所以就可 ...
- jvm内存区域
概述 jvm内存分为几个区域: 程序计数器 虚拟机栈 本地方法栈 堆 方法区 运行时常量池 直接内存 这些内存区域是在java进程中细分的,为java程序提供服务 不同的区域存储的内容不一样,生命周期 ...
- jquery 金额转换成大写
<script language="javascript" type="text/javascript"> function Ara ...
- 拥抱.NET Core,如何开发跨平台的应用并部署至Ubuntu运行
之前写了一篇博文宣布Rabbit Rpc跨平台了"拥抱.NET Core,跨平台的轻量级RPC:Rabbit.Rpc",在过程中尝试了如何编写支持跨平台的类库与应用程序,也尝试了在 ...
- Spring:Aop before after afterReturn afterThrowing around 的原理
在写这篇文章前,在网上看了一下大多数的文章,在说这一块时,都是用语言来表达的.before.after.afterReturn.afterThrowing 这四个用语言是可以说清楚的,但 around ...