题目:有一个m * n 的方格,如下图,一个小robot希望从左上角走到右下角,共有多少种不同的路线走法。

思路:

我的错误思路:全排列,从(0,0)走到(m - 1,n - 1)共需要往下走m-1步,往右走n-1步。那么计算公式就是(全排列(m-1+n-1)/(全排列(m-1)*全排列(n-1)))。想到这里我很happy的把全排列写好,submit, runtimeError。后面检测出是数值溢出了,即使使用long型数据接收全排列结果也会溢出。当然Java提供了BigInteger类帮助大数据的处理,但是这道理显然不是考察这个考点。故没有尝试BigInteger。

求助网络。第一道DP题,非常开心,当然我没有想出来,终于开始DP了。

对于任意一个目标点(假设到达这个点的路径有path_red条),如上图红框处,到达这个点都只有两条路,要么从上面的黑箭头下来(path_top),要么从左面的黑箭头过去(path_left),则可得到一个等式 path_red = path_top + path_left。核心公式就这个。

代码:

1、用最直观的回溯法:

     int uniquePathsBackTrack(int m, int n) {
if(m==1 || n==1) return 1;
return uniquePaths(m-1, n) + uniquePaths(m, n-1);
}

这个会超时,我对回溯法不了解,不明白为什么会超时。

2、最标准的DP,没有丝毫的改进。AC了。

     public int uniquePaths(int m, int n) {
if(m == 1 || n == 1) return 1; int[][] path = new int[m][n];
for(int i = 0 ; i < m ; i++) path[i][0] = 1;
for(int j = 0 ; j < n ; j++) path[0][j] = 1;
for(int i = 1 ; i < m ; i++){
for(int j = 1 ; j < n ; j++){
path[i][j] = path[i][j-1] + path[i-1][j];
}
}
return path[m - 1][n - 1];
}

还有很多对这个DP代码进行改进的,此处先放下,现在的理解不适合对DP过多纠缠。自由再回来复习的时候。

[leetcode]_Unique Paths的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. leetcode先刷_Unique Paths II

    那么上述问题,假设这个矩阵堵塞障碍,不能在若干组合前面所用的方法,因为这么多的位置实际上是没有办法的事儿. 还有剩下的唯一合理的解决方案dp该.与最低要求,并且等,从右下角以前突起,对于位置(i, j ...

  3. leetcode第一刷_Unique Paths

    从左上到右下,仅仅能向右或向下,问一共同拥有多少种走法. 这个问题当然能够用递归和dp来做,递归的问题是非常可能会超时,dp的问题是须要额外空间. 事实上没有其它限制条件的话,这个问题有个非常easy ...

  4. [LeetCode] Unique Paths II 不同的路径之二

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

  5. [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 ...

  6. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  7. [LeetCode] All Paths From Source to Target 从起点到目标点到所有路径

    Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and re ...

  8. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  9. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

随机推荐

  1. [Java] File类的常用操作

    package test.file; import java.io.File; import java.io.IOException; public class TestFile { public s ...

  2. pymongo带认证连接mongo

    import pymongo connection = pymongo.MongoClient("127.0.0.1") connection.database.authentic ...

  3. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  4. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

  5. convert NameValueCollection/Dictionary<string, object> to JSON string

    public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...

  6. MFC学习 文件操作注册表操作

    c读写文件 void CFileView::OnRead() { FILE *pFile = fopen("1.txt", "r"); /*char ch[10 ...

  7. rlwrap(在sqlplus下使用上下键)

    一:安装readline OS的安装光盘里提供了readline包. # RHEL 4 [root@oracle11g ~]# rpm -Uvh readline* error: Failed dep ...

  8. Epic - Desirable Number

    A number is called 'desirable' if all thedigits are strictly ascending eg: 159 as 1<5<9. You k ...

  9. 返回顶部js

    backToTop.js: (function () { var $backToTopEle = $('<div class="backToTop"></div& ...

  10. 关键字 new 的作用

    ①做运算符 用于创建对象和调用构造函数,小栗子a如下: Class1 obj = new Class1(); 创建匿名类型的实例,小栗子b如下: var query = from cust in cu ...