LintCode : Unique Paths II
2016-05-15 21:03
465 查看
Problem Description:
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
Code:
View Code
Hint:
Same as Unique Path, but you have to pay attention to situation where you can not get to there.
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
1and
0respectively in the grid.
Code:
public class Solution { /** * @param obstacleGrid: A list of lists of integers * @return: An integer */ public int uniquePathsWithObstacles(int[][] obstacleGrid) { // write your code here if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0){ return 0; } int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[][] paths = new int[m] ; for (int i = 0; i < m; i++) { if (obstacleGrid[i][0] == 0) { paths[i][0] = 1; } else { break; } } for (int i = 0; i < n; i++) { if (obstacleGrid[0][i] == 0) { paths[0][i] = 1; } else { break; } } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (obstacleGrid[i][j] == 0) { paths[i][j] = paths[i - 1][j] + paths[i][j - 1]; } else { paths[i][j] = 0; } } } return paths[m - 1][n - 1]; } }
View Code
Hint:
Same as Unique Path, but you have to pay attention to situation where you can not get to there.
相关文章推荐
- In-memory Computing with SAP HANA读书笔记 - 第七章:Business continuity and resiliency for SAP HANA
- XueXX and Chessboard(中南OJ)
- UI控件--自定义SeekBar样式
- LintCode : Unique Paths
- XueXX and P-P String(中南OJ)
- 【37】String,StringBuffer,StringBuilder区别和概念
- 【37】String,StringBuffer,StringBuilder区别和概念
- 【37】String,StringBuffer,StringBuilder区别和概念
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
- CharSequence类型
- android UI——绘制加减号按钮
- iOS基本图形绘制--UIBezierPath
- android UI——2D绘图绘制几何图形
- 给UILabel 或者 UIButton标题加下划线
- 在spark中使用Hive报错error: not found: value sqlContext
- JSP内置对象--request对象
- iOS开发系列--UITableView全面解析
- 按钮添加活动指示器
- 关于hibernate中的错误:Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: tb_e
- poj 1141 Brackets Sequence