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的更多相关文章

  1. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. LeetCode OJ 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. 【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 ...

  5. LeetCode OJ:Unique Paths II(唯一路径II)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. 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 ...

  7. [leetcode DP]62.Unique Paths

    判断一个物体从左上角到右下角有多少种走法 class Solution(object): def uniquePaths(self, m, n): flag = [[1 for j in range( ...

  8. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

随机推荐

  1. 关于wkwebview

    一.引入库 #import <WebKit/WebKit.h> 二.初始化有两种方式 // 默认初始化 - (instancetype)initWithFrame:(CGRect)fram ...

  2. ecstore在MySQL5.7下维护报错WARNING:512 @ ALTER IGNORE TABLE

    ecstore在MySQL5.7下维护报错WARNING:512 @ ALTER IGNORE TABLE 打开 /app/base/lib/application/dbtable.php , 替换A ...

  3. 博客停写,搬家到www.54kaikai.com

    博客搬家到自己的网站了www.54kaikai.com欢迎访问.

  4. 接口速度慢问题查找(TTFB时间长)

    前些天自己写了一个网站,但是发现接口的速度按超级慢,业务逻辑并不复杂,原因究竟在哪呢? 首先说一下,我的数据库和项目均在同一台服务器上,按道理来说,接口访问本地的数据库应该会很快才对. 后来我发现线上 ...

  5. HDU 1040 As Easy As A+B(排序)

    As Easy As A+B Problem Description These days, I am thinking about a question, how can I get a probl ...

  6. oc 导航栏跳转指定界面

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...

  7. Textarea自适应高度 JS实现,兼容IE6\7\8\9\10\11

    <!DOCTYPE html> <html> <head> <title>autoresizing textarea</title> < ...

  8. sql server 2008 R2 压缩备份数据库

    今天需要把一个省外项目的数据库从服务器上备份.拷贝到本机(跨地域传输数据库备份文件). 连上VPN,通过远程桌面连接,连接上服务器,发现数据库文件已经有20G以上大小了. 文件太大,公司网络也不稳定, ...

  9. 树莓派+Android Things

    在开始之前 谷歌前不久发布了Android Things面向物联网的系统,用意是想让android开发者用原来开发app的方式开发硬件相关的应用,扩展了android开发的方向和前景,而谷歌的Andr ...

  10. Java内存一致性

    问题 前段时间在做服务注册发现的时候,有一处这样的逻辑,先发现下游服务,然后再是服务启动,服务启动完毕才能注册服务,因为注册一个在启动过程中的服务显然是不正确的设计. 然而很不巧,我们目前使用的TTh ...