leetcode@ [62/63] Unique Paths II
2015-10-23 14:53
507 查看
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { if(obstacleGrid.size()==0 || obstacleGrid[0].size()==0) return 0; int m = obstacleGrid.size(), n = obstacleGrid[0].size(); if(obstacleGrid[m-1][n-1]==1) return 0; vector<vector<int> > dp(m); for(int i=0;i<dp.size();++i) dp[i].resize(n); for(int i=0;i<dp.size();++i){ for(int j=0;j<dp[i].size();++j) dp[i][j]=0; } dp[0][0] = (obstacleGrid[0][0]==1) ? 0 : 1; for(int i=1;i<dp.size();++i){ if(dp[i-1][0] && !obstacleGrid[i][0]) dp[i][0] = 1; } for(int j=1;j<dp[0].size();++j){ if(dp[0][j-1] && !obstacleGrid[0][j]) dp[0][j] = 1; } for(int i=1;i<dp.size();++i){ for(int j=1;j<dp[i].size();++j){ if(i>=1 && !obstacleGrid[i-1][j]) dp[i][j] += dp[i-1][j]; if(j>=1 && !obstacleGrid[i][j-1]) dp[i][j] += dp[i][j-1]; } } return dp[m-1][n-1]; } };
相关文章推荐
- GUI实例一
- IOS UI控件 - UIImageView
- does not contain bitcode. You must rebuild it with
- easyui弹出提示框
- Password and U option must have a non-empty value
- DOM API querySelector与querySelectorAll的用法
- getting NSDictionary value
- UITabelView中cell点击展开功能设计
- MEMO:UIButton 中的图片和标题 左对齐
- CruiseControl.net
- UItbleview 设置滑动范围
- easyUI使用实例讲解
- UE技巧
- UITabelView的自适应布局
- UIView的Touch事件UIControlEvents详解
- 错误The request sent by the client was syntactically incorrect ()的解决
- 8.2.1.19 Optimizing LIMIT Queries 优化LIMIT 查询:
- js中的true,false盲点
- Gene.Codes.Sequencher.v5.4.44511 1CD DNA序列分析序列拼接软件
- [转] 再探java基础——break和continue的用法