[Leetcode Week12]Unique Paths
Unique Paths 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/unique-paths/description/
Description
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.
Solution
class Solution {
int combinatorial(int big, int small) {
unsigned long long res = 1;
unsigned long long i = static_cast<unsigned long long>(big - small + 1);
unsigned long long j = 1;
for (; i <= big, j <= small; i++, j++) {
res *= i;
res /= j;
}
return static_cast<int>(res);
}
public:
int uniquePaths(int m, int n) {
return combinatorial(m + n - 2, min(m, n) - 1);
}
};
解题描述
这道题目其实只需要使用简单的数学求解。机器人每次只能向右或者向下走一步,如果是在m * n的网格中,则向右走要m - 1步,向下走要n - 1步。所以求所有路径的数目其实就是求一个组合数。即在m + n - 2步中取m - 1步。然后主要的问题是计算上面的问题:第一次WA是因为大数计算溢出。于是换了个算法,改成每次乘上一个数之后要除去一个数,而不是连乘后连除;第二次WA是因为从大数方向往小数方向进行计算的时候会出现不能整除的情况,只能是换成从小数向大数进行累积计算。
[Leetcode Week12]Unique Paths的更多相关文章
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 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(所有不同的路径)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [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 ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
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 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【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 ...
随机推荐
- Bootstrap如何适配移动浏览器
移动设备优先 1.由meta标签决定的 <meta name="viewport" content="width=device-width, initial-sca ...
- 关于__name__=='__main__
if __name__=='__main__' : 为了区分你是主动执行这个脚本,还是从别的地方把它当做一个模块去调用. 如果是主动执行,则执行.如果是调用的,则不执行主体. 里面存放的可能是一些测 ...
- 38 一次 redis 连接泄露的原因 以及 ShardedJedisPool
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011039332/article/details/85381051前言 这个是接着 上次的 这篇文 ...
- Android 数据库存储之db4o
在Android中,使用数据库除了可以使用Android内嵌的SQLite,还可以使用db4odb4o是嵌入式的面向对象的数据库,是基于对象的数据库,操作的数据本身就是对象.特点:对象以其本身的方式来 ...
- BZOJ1090:[SCOI2003]字符串折叠——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1090 Description 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S=S 2 ...
- NOIP2015Day2T3运输计划(二分+树上差分)
做了这么多NOIPTG的题,这是唯一 一道一眼秒的T3(有时候T2还不会做QAQ)... 题目大意就不说了QWQ 思路大概是:啊最大值最小化,来个二分.检验mid的话,显然就是用最长路径减去所有边权& ...
- python3.5安装pycrypto
在python中使用AES加密是一种有效的加密方式,如果你研究过微信公众号api就会发现,它也用的是这个加密的.在写代码的时候,要安装crypto模块,在linux或者mac上都好说,但是在windo ...
- apache和IIS共享80端口解决办法
第一步:把iis所发布的网站默认端口由80改为8080:第二步:修改apache的httpd.conf配置文件. 首先,要让apache支持转发也就是做iis的代理那么就要先启 用apache的代理模 ...
- 2017-7-19-每日博客-关于Linux下的CentOS中文件夹基本操作命令.doc
CentOS中文件夹基本操作命令 文件(夹)查看类命令 ls--显示指定目录下内容 说明:ls 显示结果以不同的颜色来区分文件类别.蓝色代表目录,灰色代表普通文件,绿色代表可执行文件,红色代表压缩文件 ...
- 警惕!Unity3D中UnityEngine.Object的一个小陷阱
先看看如下C#的脚本代码: 猜猜控制台打出来的是什么? In the bool parameter function, value info is: True 肯定出乎很多人的意料吧? transf ...