leetcode 63. Unique Paths II
2016-04-15 20:03
495 查看
题意
在一个矩阵中只能向右和向下走且不能走有障碍的点,问从起点到终点有多少种走法题解
使用动态规划。dp[i][j] = 0; (obstacleGrid[i][j] == 1)
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
代码
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); if(obstacleGrid[0][0] == 1) return 0; int dp[m] ; dp[0][0] = 1; for(int j = 1; j < n; j++) { if(obstacleGrid[0][j] == 1) dp[0][j] = 0; else dp[0][j] = dp[0][j - 1]; } for(int i = 1; i < m; i++) { if(obstacleGrid[i][0] == 1) dp[i][0] = 0; else dp[i][0] = dp[i - 1][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]; } };
相关文章推荐
- DocumentBuilder.parse的问题。
- 62. Unique Paths
- 详细解析BluetoothAdapter的详细api
- Android UI组件
- POJ 2785 4 Values whose Sum is 0
- iOS UITableViewCell重用问题
- leetcode 62. Unique Paths
- UITableViewCell左滑动删除 和cell上按钮的点击事件
- UIScrollView
- String和StringBuilder和StringBuffer三兄弟
- hdu 5538 House Building(长春现场赛——水题)
- UITableView常见 UI 问题总结
- confluence 安装部署
- ios UIImagePickerController简单说明
- MUI_Ajax
- requests的介绍和学习
- UI控件--RecyclerView(1)
- UITextField实现过滤选中状态拼音
- OC_实现UITableviewCell之间的间距
- UILabel自动调整大小(高度,宽度)