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 ...
随机推荐
- Storm-源码分析-Topology Submit-Executor
在worker中通过executor/mk-executor worker e, 创建每个executor (defn mk-executor [worker executor-id] (let [e ...
- lombok插件使用
1.1 lombok介绍 lombok 是一个可以帮助我们简化java代码编写的工具类,尤其是简化javabean的编写,可以通过采用注解的方式,消除代码中的构造方法,getter/setter等代码 ...
- linux里的CPU负载
昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...
- MyBatis动态代理查询出错
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache. ...
- 转!!mysql 查询条件不区分大小写问题
做用户登录模块时,输入用户名(大/小写)和密码 ,mysql都能查出来.-- mysql查询不区分大小写. 转自 http://blog.csdn.net/qishuo_java/article/de ...
- sql server性能调优
转自:https://www.cnblogs.com/woodytu/tag/%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98%E5%9F%B9%E8%AE%AD/defaul ...
- java 多线程 day02 定时器
package com.czbk.thread; import java.util.Date;import java.util.Timer;import java.util.TimerTask; /* ...
- 请求库之requests
一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内 ...
- C#转义字符(好记性不如烂笔头)
C#转义字符: ·一种特殊的字符常量:·以反斜线"\"开头,后跟一个或几个字符.·具有特定的含义,不同于字符原有的意义,故称“转义”字符.·主要用来表示那些用一般字符不便于表示的控 ...
- PAT 天梯赛 L1-013. 计算阶乘和 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-013 AC代码 #include <iostream> #include <cstdio&g ...