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 ...
随机推荐
- ansible 常用模块
http://www.linuxidc.com/Linux/2015-02/113068.htm
- 【kAri OJ】wzt的树
时间限制 1000 ms 内存限制 65536 KB 题目描述 改革春风吹满地,中国人民真争气!家庭联产承包责任制以后,全国人民争想发家致富.wzt于是包了一个山头来种植金丝楠木,花了好几年种了N棵树 ...
- CentOS下crontab执行java程序
阿里云CentOS收不到邮件 在crontab里配置执行脚本,脚本用来执行java程序,死活不执行.单独执行脚本可以运行. 查看crontab的日志文件,/var/log/cron,发现没有收到cro ...
- BZOJ-1875 HH去散步 DP+矩阵乘法快速幂
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1196 Solved: 553 [Submit][Statu ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- DLUTOJ 1331 Maximum Sum
传送门 Time Limit: 1 Sec Memory Limit: 128 MB Description You are given an array of size N and anothe ...
- 使用Android Studio搭建Android集成开发环境
有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...
- Protocol Buffer技术详解(C++实例)
Protocol Buffer技术详解(C++实例) 这篇Blog仍然是以Google的官方文档为主线,代码实例则完全取自于我们正在开发的一个Demo项目,通过前一段时间的尝试,感觉这种结合的方式比较 ...
- The illustrated guide to a Ph.D.
- WINDOWS渗透与提权总结(2)
vbs 下载者: 01 1: 02 03 echo Set sGet = createObject("ADODB.Stream") >>c:\windows\cft ...