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 ...
随机推荐
- Struts2 标签分类
- A星寻路算法
A星寻路算法 1.准备一个close关闭列表(存放已被检索的点),一个open开启列表(存放未被检索的点),一个当前点的对象cur 2.将cur设成开始点 3.从cur起,将cur点放入close表中 ...
- Java异常分类
一.基本概念 看java的异常结构图 Throwable是所有异常的根,java.lang.ThrowableError是错误,java.lang.ErrorException是异常,java.lan ...
- Springside学习
http://blog.chinaunix.net/uid-122937-id-3935052.html [一]Maven + Eclipse + springside4安装与配置 Maven安装与配 ...
- BZOJ4195 程序自动分析
Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...
- HDU 1060 Left-most Digit
传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- std::thread join和detach区别
thread detach, join 线程有两种状态,joinable或者detachable,pthread默认创建的线程是joinable的,也可以指定atrribute创建成一个detacha ...
- ResultSet
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码Statement stmt = con. ...
- linux下python版webshell后门查杀工具
使用说明: 1.查杀指定路径:python webshell.py 路径 2.按时间查找文件:python webshell.py 路径 “2013-09-28 00:00:00″ # -*- cod ...
- Java中使用split、sort函数
public static void main(String[] args) { // TODO Auto-generated method stub String str = null ; Scan ...