Unique Paths leetcode java
题目:
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?

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
题解:
其实跟爬梯子挺类似的,按个就是只能往上爬,这个就是方向可以换了下。同样想法动态规划。
分析方法也一样的,想想要到最右下角。到达右下角的方法只有两个,从上面往下,和从右面往左。
利用到达终点的唯一性,就可以写出递推公式(dp[i][j]表示到坐标(i,j)的走法数量):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
初始条件的话,当整个格子只有一行,那么到每个格子走法只有1种;只有一列的情况同理。
所以,理解的这些,代码就非常好写了。
通常来讲,我们会初始dp数组为dp[m+1][n+1]。但是这里的话,因为dp[i][j]是表示坐标点,所以这里声明dp[m][n]更容易理解。
代码如下:
1 public static int uniquePaths(int m, int n){
2 if(m==0 || n==0) return 0;
3 if(m ==1 || n==1) return 1;
4
5 int[][] dp = new int[m][n];
6
7 //只有一行时,到终点每个格子只有一种走法
8 for (int i=0; i<n; i++)
9 dp[0][i] = 1;
// 只有一列时,到终点每个格子只有一种走法
for (int i=0; i<m; i++)
dp[i][0] = 1;
// for each body node, number of path = paths from top + paths from left
for (int i=1; i<m; i++){
for (int j=1; j<n; j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
Unique Paths leetcode java的更多相关文章
- Unique Paths [LeetCode]
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Unique Paths ——LeetCode
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 114. Unique Paths [by Java]
Description A robot is located at the top-left corner of a m x n grid. The robot can only move eithe ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Java for LeetCode 062 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 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode第[62]题(Java):Unique Paths 及扩展
题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...
随机推荐
- 【WIN10】移植opencc到WIN10-UWP,實現自己的繁簡轉換工具
花了週末兩天時間,將opencc移植成WIN10-UWP可用的庫,並完成自己的繁簡轉換工具. 我的繁簡轉換工具下載地址為:https://www.microsoft.com/store/apps/9n ...
- notepad++ 如何选择10000行-20000行之间的文本?
最近要上传导入一批数据,但是数据太多,一次上传不了,所以就要分批上传,而且数据全部在一个txt里面,这时就想一次复制一部分出来导入,直到导入完成,但是问题来了,数据太多,选择1到10000行,鼠标要拉 ...
- BZOJ4242 : 水壶
对于任意两个建筑物,以它们之间的最短路为边权求出最小生成树. 则询问(x,y)的答案为最小生成树上x到y路径上边权的最大值. BFS求出离每个点最近的建筑物以及到它的距离,可以发现只有交界处的边才有用 ...
- 14、Redis的复制
写在前面的话:读书破万卷,编码如有神 --------------------------------------------------------------------------------- ...
- c# dapper mysql like 参数化
//拼接sql语句: if (!string.IsNullOrEmpty(model.Email)) { where += " and a.email like @email "; ...
- 【转载】Android dip,px,pt,sp 的区别
dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素. ...
- LPC-LINK 2 LPC4337 TQFP144 IO
- Access restriction: The method XXX from the type XXX is not accessible due to restriction XXX
插件重构的时候 遇到这个问题 Access restriction: The method setDefaultAutoCommit(boolean) from the type BasicDataS ...
- systemtap 调试postgrel
http://blog.163.com/digoal@126/blog/static/16387704020137140265557/ dtrace http://blog.163.com/dig ...
- having只用来在group by之后,having不可单独用,必须和group by用。having只能对group by的结果进行操作
having只能对group by的结果进行操作 having只能对group by的结果进行操作 having只能对group by的结果进行操作 having只用来在group by之后,havi ...