题目:有一个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. 91、sendToTarget与sendMessage

    Message msg = handler.obtainMessage();               msg.arg1 = i;               msg.sendToTarget(); ...

  2. java获取本机IP地址和MAC地址的方法

    // 获取ip地址 public static String getIpAddress() { try { Enumeration<NetworkInterface> allNetInte ...

  3. Using Redis as Django's session store and cache backend

    http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/

  4. 初次体验架设PHP网站

     最近需要快速架设一个网站,因此淘了一份成型的模板,然后就开始..过程比较曲折. 测试环境:Win7旗舰+IIS7.5+mysql5.6+PHP5.2.17+PHPManagerForIIS-1.1. ...

  5. (转)C# MD5

    本文原地址:http://blog.csdn.net/zhoufoxcn/article/details/1497099 作者:周公 代码如下: using System; using System. ...

  6. 共享一个MVC通过NPOI导出excel的通用方法

    public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, param ...

  7. Spark ThriftServer使用的大坑

    当用beeline连接default后,通过use xxx切换到其他数据库,再退出, 再次使用beeline -u jdbc:hive2://hadoop000:10000/default -n sp ...

  8. Grunt 之通配符

    在描述源码路径的时候,经常有一些特殊的奇怪的要求.Grunt 通过内建的 node-glob 和 minimatch 库提供了文件名的扩展机制. 常见的通配符如下: * 匹配除了 / 之外的任意数量的 ...

  9. (转载)MonoBehaviour的事件和具体功能总结

    分享一点MonoBehaviour的事件和具体功能总结的基础知识,苦于Visual Studio 2013没有对MonoBehaviour的行为做出智能提示,写个函数都要全手打,记性好的将就着手打,脑 ...

  10. 翻译:wiki中的business logic词条

    Business logic 业务逻辑 From Wikipedia, the free encyclopedia 来自Wikipedia,自由的百科全书 In computer software, ...