63. Unique Paths II
来源:互联网 发布:tcp网络编程java 编辑:程序博客网 时间:2024/06/14 19:32
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
.
Note: m and n will be at most 100.
DP求解,与62题类似,只是本题设置了障碍物,程序如下所示:
class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int row = obstacleGrid.length, col = obstacleGrid[0].length; int[][] matrix = new int[row][col]; for (int i = 0; i < row; ++ i){ if (obstacleGrid[i][0] == 1){ break; } matrix[i][0] = 1; } for (int i = 0; i < col; ++ i){ if (obstacleGrid[0][i] == 1){ break; } matrix[0][i] = 1; } for (int i = 1; i < row; ++ i){ for (int j = 1; j < col; ++ j){ if (obstacleGrid[i][j] == 1){ continue; } matrix[i][j] = matrix[i-1][j] + matrix[i][j-1]; } } return matrix[row-1][col-1]; }}
阅读全文
0 0
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- Python学习笔记——使用dict和set
- 叫教你打造一个滑动悬浮置顶的视觉效果,给你的广告栏增加一些特色
- 常见编码GBK、GB2312、UTF-8、ISO-8859-1的区别
- linux centos7 安装nginx
- 每日练习_01
- 63. Unique Paths II
- iframe标签——HTML中的“画中画”
- JSON Web Token
- M
- JS实现信息滚动制作
- Java程序性能优化 读书笔记(六)设计模式:观察者模式
- day06-学习
- String类常用方法源码分析
- MySQL学习笔记(六):MySQL中查看和修改表的存储引擎