Leetcode 63. Unique Paths II 路径搜寻2 解题报告
2016-05-05 23:56
501 查看
1 解题思想
首先这是昨天的升级版,请先看这里Leetcode #62. Unique Paths 路径搜寻 解题报告
这道题呢,加了一个小困难,就是有的地方是不能走的,使用对应矩阵当中1来标示。
然而呢,这也并没有什么难的,做法还是和之前的那个一样,只是记得加个if,如果上边或者左边,那么就不加那一部分就可以,把它当做0.剩下还真没什么不同
2 原题
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.
3 AC解
public class Solution { /** * 其实就是加个障碍判断罢了。。。 * */ public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m=obstacleGrid.length; int n=obstacleGrid[0].length; int[][] dp=new int[m] ; int i=0,j; while(i<n && obstacleGrid[0][i]==0){ dp[0][i++]=1; } i=0; while(i<m && obstacleGrid[i][0]==0){ dp[i++][0]=1; } for(i=1;i<m;i++){ for(j=1;j<n;j++){ if(obstacleGrid[i][j]==0) dp[i][j]=dp[i-1][j]+dp[i][j-1]; } } return dp[m-1][n-1]; } }
相关文章推荐
- java.sql.SQLException: QueryRunner requires a DataSource to be invoked in this way, or a Connection
- UILable常用属性和方法
- Android 中文API:Running Gradle Builds
- UITableView 02
- GUI
- android 6.0 permission exception Can only use lower 8 bits for requestCode
- 实现当UILable的内容超出其范围后自动滚动效果
- KVC中setValuesForKeysWithDictionary
- POJ 1986 Distance Queries tarjan求LCA
- UITextField限制汉字数量最正确的姿势,解决iOS7下substringToIndex方法导致的崩溃
- UITouch
- FluentData 学习 第一弹
- 通过key-value更改按钮的圆角
- Addressing Complex and Subjective Product-Related Queries with Customer Reviews-www2016-20160505
- 【BZOJ 1568】【JSOI 2008】Blue Mary开公司
- 实用知识:<AddressBookUI/AddressBookUI.h>(获取手机通讯录)
- Android stuido快捷键集合
- Top K Frequent Elements
- Android Bluetooth(蓝牙)实例
- 【LeetCode-334】Increasing Triplet Subsequence