LeetCode OJ 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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
【题目分析】
给定一个m*n的grid,机器人从左上角出发到达右下角,每次只能向右或者向下移动一步,则到达右下角的不同的路线有多少条?
【思路】
1. 组合数学的思想
在组合数学中关于这个问题有一个专门的讨论,作为组合数入门的一个引入题目。原题目是从坐标点(0, 0)出发,到达另外一个点(m, n)的不同路径有多少条,其中m >= 0, n>=0。
组合数学的思想是这样的,从(0, 0)到达(m, n),无论无论怎么走,总的步数是确定的:m + n,而且肯定是向右移动了m步,向上移动了n步。那么我们先选定向右移动的m步,则剩下的n步就是确定的。所以这是一个组合数C(m+n, m) 或者 C(m+n, n)。
2. 动态规划的思想
动态规划的思想就是在移动过程中计算出到达的每一个小格子的方法数。
(1)到达最上边和最右边方格的方法数是1;
(2)以后到达每一个方格的方法数由它左边和上边的方法数确定,因为我们只能向右和向下移动;结果如下:
【java代码】组合法——由于计算过程中可能会遇到很大的数,要注意溢出的问题。
public class Solution {
public int uniquePaths(int m, int n) {
m--; n--;
int mn = m + n;
int num = Math.min(m ,n);
double ans = 1;
for(int i=0;i<num;i++)
ans = ans * ((double)(mn - i) / (num-i));
return (int)Math.round(ans);
}
}
【java代码】动态规划
public class Solution {
public int uniquePaths(int m, int n) {
int[] dp = new int[m];
dp[0] = 1;
for (int i = 0; i < n; i++)
for (int j = 1; j < m; j++)
dp[j] = dp[j - 1] + dp[j];
return dp[m - 1];
}
}
LeetCode OJ 62. Unique Paths的更多相关文章
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【LeetCode】62. Unique Paths
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode OJ:Unique Paths II(唯一路径II)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 DP]62.Unique Paths
判断一个物体从左上角到右下角有多少种走法 class Solution(object): def uniquePaths(self, m, n): flag = [[1 for j in range( ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
随机推荐
- 洛谷-神奇的幻方-NOIP2015提高组复赛
题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,--,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. ...
- Symfony没有安装依赖_PHP Fatal error: require(): Failed opening required
$ php bin/console server:run PHP Warning: require(D:\home\workspace\pd\app/../vendor/autoload.php): ...
- C# 压缩打包文件下载
C# 压缩打包文件下载 public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform { #region INam ...
- Struts2 网站上来多少人
strut.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBL ...
- jmeter里json path postprocessor的用法
后置处理器添加 json path postprocessor. 用处: 当前接口响应返回的json中提取内容,作为变量可以在不同的请求中传递. 如下,从登陆接口返回的json中提取user id,变 ...
- 利用poi向excle写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org ...
- [Rails] 从 Request 到 Response(1)
本文翻译自:Rails from Request to Response 系列:个人选择了自己感兴趣的部分进行翻译,需要阅读原文的同学请戳前面的链接. 第一部分 导言(Introduction) 服务 ...
- [转]查看Android源码版本
有时候我们辛苦取到Android的源代码,想知道它的确切版本号,比如有时候我们只粗略知道拿到的是2.3的源码,但并不明确他的小版本号,这就是有时候明明都是2.3的代码可比较起来还是有差异的原因,比方说 ...
- Linq入门
一.Linq需要的C#语法支持: 1.隐式变量的使用var var使用时必须初始化 var是强类型数据 2.自动属性:public string FirstName{get ;set;} 3 ...
- box2d 已知bug
1.动态刚体与一个与静态刚体重叠的小的感应刚体在contactBegin时,有些时候无法侦测到