LeetCode 63. Unique Paths II
来源:互联网 发布:windows xp sp3原版 编辑:程序博客网 时间:2024/06/06 14:22
题意
给出一个
思路
和上一题62. Unique Paths差不多,只是多了一些障碍,只需要判断一下就可以了,需要注意对于边界的处理如果前一步不能到达,那么当前也是不可能到达的.
代码
class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); if(m == 0) return 0; int n = obstacleGrid[0].size(); vector<vector<int> >dp(m, vector<int>(n)); for(int i = 0; i < m; i++){ dp[i][0] = (obstacleGrid[i][0] == 0); if(i && dp[i-1][0] == 0) dp[i][0] = 0; } for(int i = 0; i < n; i++){ dp[0][i] = (obstacleGrid[0][i] == 0); if(i && dp[0][i - 1] == 0) dp[0][i] = 0; } for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ if(obstacleGrid[i][j] == 1){ dp[i][j] = 0; } else{ dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } } return dp[m - 1][n - 1]; }};
阅读全文
0 0
- LeetCode --- 63. Unique Paths II
- LeetCode 63.Unique Paths II
- [leetcode] 63.Unique Paths II
- [leetcode] 63.Unique Paths II
- Leetcode 63. Unique Paths II
- LeetCode 63. Unique Paths II
- [leetcode] 63. Unique Paths II
- leetcode 63. Unique Paths II
- 63. Unique Paths II LeetCode
- LeetCode 63. Unique Paths II
- [LeetCode]63. Unique Paths II
- leetcode 63. Unique Paths II
- LeetCode *** 63. Unique Paths II
- leetcode 63. Unique Paths II
- Leetcode:63. Unique Paths II
- LeetCode 63. Unique Paths II
- leetcode 63.Unique Paths II
- LeetCode 63. Unique Paths II
- 两数组的交 II
- 九度1033:继续xxx定律
- MySQL-sql的逻辑查询处理
- Node.JS全开源B2C商城
- View事件分发机制2
- LeetCode 63. Unique Paths II
- 发送有序广播案例
- Unknown Treasure ————(hdu5446) 中国剩余定理+卢卡斯定理
- Android7 mediacodec SEI
- 一次解决问题的经过
- 封装自己的printf函数
- 获取项目中Classpath中的资源路径
- Android TabHost的学习笔记
- mybatis动态SQL标签的用法