LeetCode 63 - Unique Paths II
2016-04-05 17:11
489 查看
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
1and
0respectively
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.
My Code
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); vector<vector<int> > grid(m, vector<int>(n, 0)); int ii = 0; for (; ii < m && obstacleGrid[ii][0] == 0; ii++) grid[ii][0] = 1; int jj = 0; for (; jj < n && obstacleGrid[0][jj] == 0; jj++) grid[0][jj] = 1; for (int i = 1; i < m; i++) for (int j = 1; j < n; j++) { if (obstacleGrid[i][j] == 1) continue; grid[i][j] = grid[i-1][j] + grid[i][j-1]; } return grid[m-1][n-1]; } };Runtime: 6 ms
相关文章推荐
- UITableView的section header view悬停的方法
- Nexus设置guest只访问指定artifact
- UIDynamic 物理动效
- UISearchBar(搜索框)的方法属性介绍
- AndroidUI之登录界面的实现分析
- UIButton内文字自适应button长度
- Android4.4 Systemui状态栏之信号逻辑流程分析
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- Android深入浅出系列之Bluetooth—蓝牙操作(二)
- Android深入浅出系列之Bluetooth—蓝牙操作(一)
- 在Web工程中用JavaMail 发送邮件时报com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required原因
- setValuesForKeysWithDictionary奔溃问题
- LeetCode 62 - Unique Paths
- 10个顶级的CSS UI开源框架
- 黄聪:No 'Access-Control-Allow-Origin' header is present on the requested resource解决办法
- iOS 9 弹窗UIalertController
- 2016.04.03,英语,《Vocabulary Builder》Unit 09
- 如何让按钮UIButton的文字左对齐?
- ServletRequest接口的主要方法
- easyui combobox 值怎样获取