leetcode63 Unique Paths II
2016-01-06 15:46
686 查看
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
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
Note: m and n will be at most 100.
View Code
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.
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int ans=0; int m=obstacleGrid.size(); if(!m) return ans; int n=obstacleGrid[0].size(); vector<int> jlist(n,0); for(int i=n-1;i>=0;i--) { if(obstacleGrid[m-1][i]==1) break; else jlist[i]=1; } for(int i=m-2;i>=0;i--) { for(int j=n-1;j>=0;j--) { if(j==n-1) { if(obstacleGrid[i][j]==1) jlist[j]=0; } else { if(obstacleGrid[i][j]==1) jlist[j]=0; else jlist[j]+=jlist[j+1]; } } } return jlist[0]; } };
View Code
相关文章推荐
- leetcode62 Unique Paths
- UINavigationController 导航控制器 IOS7适配 导航栏不透明
- leetcode60 Permutation Sequence
- UICollectionView的重排功能
- iOS UI学习笔记(五)UITextField
- HDU 4372 Count the Buildings 组合数学
- mysql sequence 生成 & 遇到的问题
- UE4 Socket多线程非阻塞通信【1】
- LA 2531 The K-league 最大流
- UI设置圆角的方向
- Packet for query is too large (12238 > 1024). You can change this value
- Xcode警告:The Copy Bundle Resources build phase contains this target's Info.plist file
- ABMultiValueCopyLabelAtIndex使用不当引起的内存泄漏
- iOS UISearchBar
- QtDesigner修改了ui文件后,程序运行不是修改后的内容。
- Java 基础之 String、StringBuilder、StringBuffer、CharSequence 区别
- IOS开发 把长度不等的UICollectionView 的cell 居左
- 关于UITableView的性能优化(历上最全面的优化分析) ...
- leetcode95 Unique Binary Search Trees II
- leetcode96 Unique Binary Search Trees