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

难度: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. java反射——构造方法

    大家都知道反射技术在Java里面时非常重要的一个技术点,因为Java好多框架的编写都是基于反射的,别的不多说,spring框架里面的IOC就是基于反射实现.那么什么是反射呢?JAVA反射机制是在运行状 ...

  2. finereport Web工具栏

    1.获取报表工具栏按钮 在报表页面中,偶尔会有需要获取工具栏按钮的需求,之前只能通过contentPane.toolbar.options.items[index]来获取,比较不方便,现在我们提供了两 ...

  3. ArcGIS runtime sdk for wpf 授权

    这两天由于runtime sdk for wpf的授权和runtime sdk 其他产品的授权的不一样导致自己混乱不堪. 总结下吧. sdk 简介 当前ArcGIS runtime sdk 包括一系列 ...

  4. [转载]js复制内容加版权声明代码

    转自:https://www.cnblogs.com/zdz8207/p/js-oncopy.html var ua = navigator.userAgent.toLowerCase(); if( ...

  5. Hibernate 中一对多和多对多映射

    1. 一对多映射 1.1 JavaWeb 一对多建表原则 多方表的外键指向一方表的主键; 1.2 编写一对多的 JavaBean // 客户(一方)和联系人(多方) // 客户(一方) JavaBea ...

  6. Java创建Timestamp的几种方式

    1.java创建Timestamp的几种方式 Timestamp time1 = new Timestamp(System.currentTimeMillis()); Timestamp time2 ...

  7. java.lang.instrument: 一个Java对象占用多少字节?

    一.对象头包括两部分信息:Mark Word(标记字段)和 Klass Pointer(类型指针)   1. Mark Word 用于存储对象自身的运行时数据,如哈希码(HashCode).GC分代年 ...

  8. Android Paint setXfermode

     背景: dst        前景: src  PorterDuff.Mode.CLEAR 清除画布上图像 PorterDuff.Mode.XOR 取两层图像的非交集部门 PorterDuff.Mo ...

  9. Hibernate缓存原理

    对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键. 简单来讲Hibernate就是对JDBC进行封装,以实现内部状态的管理,OR关系的映射等, 但随之带来的就是数据访问 ...

  10. Mahout介绍-炼数

    Mahout的中文含义:象夫