LeetCode第[62]题(Java):Unique Paths 及扩展
题目:唯一路径(机器人走方格)
难度: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 及扩展的更多相关文章
- 【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 ...
- 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 + ...
- 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 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【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 ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- jsp 通用获取所有表单值传后台
新建一个js文件,自定义一个jquery 函数. 在jsp页面引用 下面为:自定义函数 $.fn.GetDivJson = function (prifix,orgModel) { var $oute ...
- Spring Context及ApplicationContext
web.xml 这是声明了一个父工厂 <context-param> <param-name>contextConfigLocation</param-name> ...
- delphi 事件记录
delphi常用事件 序号 事件 描述 1. OnActive 焦点称到窗体或控件时发生 2. OnClick 鼠标单击事件 3. OnDbClick 鼠标双击事件 4. OnClose和OnClos ...
- 第05章—Swagger2打造在线接口文档
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- 我的Android进阶之旅------>Android APP终极瘦身指南
首先声明,下面文字转载于: APK瘦身实践 http://www.jayfeng.com/2015/12/29/APK%E7%98%A6%E8%BA%AB%E5%AE%9E%E8%B7%B5/ APP ...
- sql 基础查询集锦
授权 GRANT All ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED ...
- (转)java类到底是如何加载并初始化的?
Java虚拟机如何把编译好的.class文件加载到虚拟机里面?加载之后如何初始化类?静态类变量和实例类变量的初始化过程是否相同,分别是如何初始化的呢?这篇文章就 是解决上面3个问题的. 若有不正之处, ...
- 聚合的安全类导航、专业的安全知识学习平台——By Me:)
以“基于对抗的安全研发”为初衷,让大家在工作中始终有安全意识.安全思维和安全习惯,几年前自己搭建了面向公司内部全员的安全晨报.现在站在“用户“的角度回头看看,觉得科目设计等很多方面都还有很多的不足: ...
- tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py
具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子: ...
- Python集合方法整理(Day9)
#作用:去重,关系运算, #定义: 知识点回顾 可变类型是不可hash类型 不可变类型是可hash类型 #定义集合: 集合:可以包含多个元素,用逗号分割, 集合的元素遵循三个原则: 1:每个元素必须是 ...