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 ...
随机推荐
- JDBC之 自增长与事务
JDBC之 自增长与事务 1.自增长 有这样一个现象:数据库中有两个表格 学生表(学生姓名,所在班级),班级表(班级号(自增长的主键),班级人数). 现在我往班级表插入一条信息, 只提供班级人数,班级 ...
- django权限管理
当我们为应用创建一个Models, 在同步到数据库里,django默认给了三个权限 ,就是 add, change, delete权限. 首先,我们创建一个perm_test的project, 然后再 ...
- Spring Data JPA使用keywords关键字实现CAST函数
对不起,经过几天几夜的使用的研究得出这种方式是无法实现的,在查询上的关键字只有这些: https://docs.spring.io/spring-data/jpa/docs/2.1.x/referen ...
- word标题编号与上一级不一致的解决方法
前段时间时间就遇到了这个问题,情形如下: 1.标题内容一 1.1二级标题 1.2二级标题 2.第二个一级标题 1.3第二个一级下的二级标题 1.4.就这样乱了 当时搜索一通后很容易就解决了……于是忘记 ...
- 《Linux设备驱动开发详解(第3版)》(即《Linux设备驱动开发详解:基于最新的Linux 4.0内核》)--宋宝华
http://blog.csdn.net/21cnbao/article/details/45322629
- Visio中如何绘制黑白图像
- 向OSG视图Viewer发送消息
句柄是以下面的方式传递给osgViewer::Viewer的,osgViewer::View.getCamera().setGraphicsContext(osg::GraphicsContext); ...
- MySQL客户端输出窗口显示中文乱码问题解决办法
最近发现,在MySQL的dos客户端输出窗口中查询表中的数据时,表中的中文数据都显示成乱码,如下图所示:
- C#获取文件夹及文件的大小与占用空间的方法
本文详细介绍了利用C#实现根据路径,计算这个路径所占用的磁盘空间的方法 . 网上有很多资料都是获取文件夹/文件的大小的.对于占用空间的很少有完整的代码.这里介绍实现这一功能的完整代码,供大家参考一下. ...
- 内存加载DLL
1.前言 目前很多敏感和重要的DLL(Dynamic-link library) 都没有提供静态版本供编译器进行静态连接(.lib文件),即使提供了静态版本也因为兼容性问题导致无法使用,而只提供DLL ...