63. Unique Paths II
2016-05-11 16:20
375 查看
题目:
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
in the grid.
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.
题意:
与Unique Paths题目类似,本题中包含一些阻碍,有阻碍的地方不能经过。空间复杂度O(m*n);
代码:
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.
题意:
与Unique Paths题目类似,本题中包含一些阻碍,有阻碍的地方不能经过。空间复杂度O(m*n);
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<vector<int>> result(m+1, vector<int>(n+1, 0)); result[0][1] = 1; for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ if(!obstacleGrid[i-1][j-1]){ result[i][j] = result[i-1][j] + result[i][j-1]; } } } return result[m] ; } };优化空间复杂度,没有使用m*n大小的数组存储所有元素的可到达路径数,使用了两个小数组。
代码:
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<int> pre(m, 0); vector<int> cur(m, 0); for(int i=0; i<m; i++){ if(!obstacleGrid[i][0]){ pre[i] = 1; }else{ break; } } for(int j=1; j<n; j++){ bool flag = false; if(!obstacleGrid[0][j]){ cur[0] = pre[0]; if(cur[0]){ flag = true; } }else{ cur[0] = 0; } for(int i=1; i<m; i++){ if(!obstacleGrid[i][j]){ cur[i] = cur[i-1] + pre[i]; if(cur[i]){ flag = true; } }else{ cur[i] = 0; } } if(!flag){ return 0; } swap(pre, cur); } return pre[m-1]; } };再次优化空间复杂度。使用一个小数组存储必要的数。
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<int> cur(m, 0); for(int i=0; i<m; i++){ if(!obstacleGrid[i][0]){ cur[i] = 1; }else break; } for(int j=1; j<n; j++){ bool flag = false; if(obstacleGrid[0][j]){ cur[0] = 0; }else flag = true; for(int i=1; i<m; i++){ if(!obstacleGrid[i][j]){ cur[i] += cur[i-1]; if(cur[i]){ flag = true; } }else cur[i] = 0; } if(!flag){ return 0; } } return cur[m-1]; } };
相关文章推荐
- runtime-运行时动态添加占位文字颜色-UITextField
- "mkimage" command not found - U-Boot images will not be built
- NGUI
- 无网或断网提示,自定义界面(UIView分类,使用方便)
- easyui-datagrid控件保存选中项,每次打开页面上次被选中的行前面都被勾选
- automaticallyAdjustsScrollViewInsets和edgesForExtendedLayout和extendedLayoutIncludesOpaqueBars
- 集成容联:Warning! ivar size mismatch in PSUICollectionView_ - can't change the superclass.解决办法
- HDU 1005 Number Sequence
- 【Arduino官方教程第一辑】示例程序 3-7 信号平滑
- 【C#枚举】根据EnumName获取Value
- deque
- iOS导航栏自动隐藏和显示属性,UINavigationController属性
- 347 Top K Frequent Elements
- easyui from 缓存问题处理
- UIAlertController 使用
- Android关于buildToolVersion与CompileSdkVersion的区别
- 62. Unique Paths
- dequeueReusableCellWithIdentifier 总是返回nil
- iOS UIDevice设备信息,传感器设置,横竖屏判断操作,手机电池操作
- 自定义UICollectionViewLayout实现瀑布流