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 ...
随机推荐
- android中MVP模式(一) - 清风明月的专栏 - CSDN博客
presenter 主持人.主导器 ====== 1. 明确需求,界面如下:可存,可根据id读取数据. 包结构图 2. 建立bean public class UserBean { private S ...
- 「BZOJ 4228」Tibbar的后花园
「BZOJ 4228」Tibbar的后花园 Please contact lydsy2012@163.com! 警告 解题思路 可以证明最终的图中所有点的度数都 \(< 3\) ,且不存在环长是 ...
- KVM源代码解读:linux-3.17.4\arch\x86\include\asm\kvm_host.h
/* * Kernel-based Virtual Machine driver for Linux * * This header defines architecture specific int ...
- 在centos中安装jenkins master为service
需要sudo或root权限. 翻译自: https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Red+Hat+dis ...
- 关于C++类型检查的一点小挫折
问题: 定义了一个float型数组Lut[],我让一个整型指针指向数组名int * Address=lut ; VS2008报错: error C2440: '=' : cannot conv ...
- Linux虚拟主机管理系统---wdcp
关于WDCP这款虚拟主机管理系统,是疯子使用的第二款Linux虚拟主机管理系统,使用是挺简单的,以前好像是因为编码问题而放弃这款面板. WDCP功能比较完善,基本上需要的功能都能满足,例如:在线下载. ...
- lodash用法系列(4),使用Map/Reduce转换
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- OCP-1Z0-051-题目解析-第22题
22. You need to create a table for a banking application. One of the columns in the table has the fo ...
- 初识安卓小程序(Android电话拨号器)
首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"电话拨号器",创建的时候点击"clipart",如图: 然后在res目录下找到layout目录,找 ...
- 反恐24小时第一季/全集24 Live Another Day迅雷下载
反恐24小时 第一至九季 24 Season 1-9 (2001-2014) 本季看点:<24小时>第8季将在拥有美国的象征自由女神像的纽约开始,在新的一天,CTU重新开张,新的领导为从M ...