题目:唯一路径(机器人走方格)

难度:Medium

题目内容

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?

翻译

机器人位于m x n网格的左上角(在下图中标记为“开始”)。

机器人只能在任何时间点移动或向右移动。机器人正试图到达网格的右下角(在下图中标记为“Finish”)。

有多少种可能的路径?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3

Example 2:

Input: m = 7, n = 3
Output: 28

我的思路:动态规划题,每个点(m,n)都是由(m-1,n)+(m,n-1)这两个点的可能数之和。最简单的方法就是用递归来实现。

我的代码:

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

我的复杂度:O(m*n)    空间复杂度也是O(m*n) ——递归深度

结果:41 / 62 test cases passed.     Time Limit Exceeded

Last executed input:   51  9

递归就是这样,代码很简单,但是运行速度很慢,稍微大一点就会超时。

答案代码

     public int uniquePaths(int x, int y) {
int dp[][] = new int[x][y];
for(int i = 0; i< x;i++){
for(int j = 0;j<y;j++){
if (i == 0 || j == 0) {
dp[i][j] = 1;
continue;
}
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[x-1][y-1];
}

答案复杂度:O(m*n)    空间复杂度也是O(m*n)  虽然复杂度都一样,但是由于用迭代替换了递归,运行速度大大提高。

答案思路:递归无非就是把本层的计算调用深一层的计算,然后将深一层的结果返回,所以如果将从第一层到最后一层所有的值都计算出来并由某种数据结构保存,那么就可以直接迭代进行计算。

所以一维的递归:一维只会与一维相关,所以一路直线迭代即可。例如:求阶乘(只与上一个有关,记录一个即可)、斐波那契数列(与上两个有关,需要记录两个)

  二维的递归:每一个元素都与两个维度相关,所以得借用二维数组来记录所有值。

扩展:当x == y的时候,此时为正方格,是否有更好的方法?

  因为此时类似于卡特兰数,此时有 f(n+1)=f(n)* (4*n-2),所以代码中加入方形判断可以优化方形的计算速度,如下:

        if (x == y) {
int ans = 1;
for (int i = 1; i < x; i++) {
ans = ans * (4*i-2)/i;
}
return ans;
}

注意:当要使用  “  *=  ”  这种符号的时候,如果右边是一个表达式且含有 除号 ,那么最好还是不要使用,因为这个运算符是先运算右边再乘自己,所以有可能右边的计算顺序就不对了。

例如当x==y==4的时候  此时ans = =6   而  i ==3 ,如果使用  ans *= (4*i -2)/i; 所以先计算右边就有   10 /  3 = 3,  然后再乘以6  最后结果为18,结果错误。

而使用 ans = ans * (4*i-2)/i; 就不会有此错误。

LeetCode第[62]题(Java):Unique Paths 及扩展的更多相关文章

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

  2. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  3. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  5. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  7. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  8. 【Leetcode】【Medium】Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. 【Leetcode】【Medium】Unique Paths II

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

随机推荐

  1. Centos7 下Boost 1.61.0源码 配置开发环境

    1 下载地址 https://sourceforge.net/projects/boost/files/boost/1.61.0/ boost_1_61_0.tar.gz 2 卸载系统自带的boost ...

  2. ES正在弱化type这个概念

    百度Elasticsearch-产品描述-介绍-百度云 https://cloud.baidu.com/doc/BES/System.html#.E5.9F.BA.E6.9C.AC.E6.A6.82. ...

  3. Java Concurrency In Practice

    线程安全 定义 A class is thread-safe if it behaves correctly when accessed from multiple threads, regardle ...

  4. 流畅的python 对象引用 可变性和垃圾回收

    对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...

  5. Mycat教程---数据库的分库分表

    mycat介绍 介绍在官方网站上有比较详细的介绍,在这里复制粘贴没什么意思,大家到官网上看 官网链接 前置条件 本教程是在window环境下运行的,实际生产推荐在Linux上运行. 必备条件(自行安装 ...

  6. go——并发

    并发与并行的区别: 并发:逻辑上具备同时处理多个任务的能力. 并行:物理上在同一时刻执行多个并发任务.通常都会说程序是并发设计的,也就是说它允许多个任务同时执行,但实际上并不一定真在同一时刻发生.在单 ...

  7. LeetCode:N叉树的后序遍历【590】

    LeetCode:N叉树的后序遍历[590] 题目描述 给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 题目分析 这道题有好几 ...

  8. 在vps主机上***

    一.安装 Shadowsocks服务端: 1.下载软件包 yum install python-setuptools && easy_install pip pip install s ...

  9. maven项目中如何创建web.xml

    在web工程创建时创建web.xml:用Eclipse新建一个web项目没有自动生成web.xml 在创建maven工程时,大多没有创建web.xml文件,web.xml路径:src/main/web ...

  10. Ubuntu 16.04 安装Django

    > pip install django==1.10.3......或者:> pip3 install django==1.10.3(我采用)......或者:>python3 -m ...