Unique Paths | & ||
Unique Paths I
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?
分析:
用A[i][j]表示到达点i,j可能的走法。 对于点A[i][j],它可以从上一个格子下来,也可以从左边那个格子过来。所以A[i][j] = A[i-1][j] + A[i][j-1].
public class Solution {
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
public int uniquePaths(int m, int n) {
if (n < || m < ) return ;
if (n == || m == ) return ;
int[][] paths = new int[m][n];
for (int i = ; i < paths.length; i++) {
for (int j = ; j < paths[].length; j++) {
if (i == || j == ) {
paths[i][j] = ;
} else {
paths[i][j] = paths[i - ][j] + paths[i][j - ];
}
}
}
return paths[m - ][n - ];
}
}
Another solution:
(For clarity, we will solve this part assuming an X+1 by Y+1 grid)
Each path has X+Y steps. Imagine the following paths:
X X Y Y X (we move right on the first 2 steps, then down on the next 2, then right for the last step)
X Y X Y X (we move right, then down, then right, then down, then right)
…
Each path can be fully represented by the moves at which we move right. That is, if I were to ask you which path you took, you could simply say “I moved right on step 3 and 4.” Since you must always move right X times, and you have X + Y total steps, you have to pick X times to move right out of X+Y choices. Thus, there are C(X, X+Y) paths (eg, X+Y choose X).
Unique Paths II
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.
分析:
原理同上,没有任何区别。
public class Solution {
/**
* @param obstacleGrid: A list of lists of integers
* @return: An integer
*/
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null || obstacleGrid.length == || obstacleGrid[].length == ) return ;
int m = obstacleGrid.length;
int n = obstacleGrid[].length;
int[][] paths = new int[m][n];
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (obstacleGrid[i][j] == ) {
paths[i][j] = ;
} else if (i == && j == ) {
paths[i][j] = ;
} else if (i == ) {
paths[i][j] = paths[i][j - ];
} else if (j == ) {
paths[i][j] = paths[i - ][j];
} else {
paths[i][j] = paths[i - ][j] + paths[i][j - ];
}
}
}
return paths[m - ][n - ];
}
}
Unique Paths | & ||的更多相关文章
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II
这题在Unique Paths的基础上增加了一些obstacle的位置,应该说增加的难度不大,但是写的时候对细节的要求多了很多,比如,第一列的初始化会受到之前行的第一列的结果的制约.另外对第一行的初始 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【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 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 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 ...
随机推荐
- poj3522 kruskal+枚举
题目的意思是求构成生成树的边的最大边和最小边的差最小.枚举即可 #include<stdio.h> #include<string.h> #include<algorit ...
- Html-input文本框只能输入数字
onKeyPress="if ((event.keyCode < 48 || event.keyCode > 57)) event.returnValue = false;&qu ...
- Java设计模式-观察者模式(Observer)
包括这个模式在内的接下来的四个模式,都是类和类之间的关系,不涉及到继承,学的时候应该 记得归纳,记得本文最开始的那个图.观察者模式很好理解,类似于邮件订阅和RSS订阅,当我们浏览一些博客或wiki时, ...
- 【poj1201】 Intervals
http://poj.org/problem?id=1201 (题目链接) 题意 给出n个区间${[ai,bi]}$,要求选出尽可能少的数,使得每个区间i中至少存在${c[i]}$个数. Soluti ...
- POJ 2828 Buy Tickets
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server
目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...
- 配置Junit测试程序
第一步:加载所需要的包:右键-->Build Path-->Configure Build Path-->Libraries-->Add Library-->Junit ...
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- eclipse中Preferences的一些设置
1.在Eclipse里面设置了java文件保存时自动格式化,在java->Code Style->Formatter里设置了自定义的格式化的样式,这样每次保存后都会自动格式化代码,用了一段 ...
- ios 判断控制器是否是第一次进入画页的做法
什么是第一次进入画页,只viewDidLoad一次: 所以只需要在viewDidLoad中加一个标识就行了. 加一个成员变量,或者属性,用来记录这个标识 一旦viewDidLoad后,这个就说明不是第 ...