leetcode 63 C++. Unique Paths II
来源:互联网 发布:php引导页源码 编辑:程序博客网 时间:2024/06/06 05:22
解题思路:
回顾一下Unique Paths
3×3矩阵,到每个格子的方法数是多少?
1 1 1
1 2 3
1 3 6
可以看到到达某个格子的路径数为它上和左的路径数的和, 也就是dp[i][j]=dp[i-1][j]+dp[i][j-1]
最后进行加和的时候,注意对第一行和第一列进行特殊处理即可
class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); int i,j; if(obstacleGrid[0][0]==1||obstacleGrid[m-1][n-1]==1)return 0; vector<vector<int>> dp(m,vector<int>(n,1)); for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(obstacleGrid[i][j]==1)dp[i][j]=0; } } for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(dp[i][j]!=0){ if(i-1<0&&j-1>=0)dp[i][j]=dp[i][j-1]; if(i-1>=0&&j-1<0)dp[i][j]=dp[i-1][j]; if(i-1>=0&&j-1>=0)dp[i][j]=dp[i][j-1]+dp[i-1][j]; } } } return dp[m-1][n-1]; }};
0 0
- [leetcode-63]Unique Paths II(c)
- leetcode 63 C++. Unique Paths II
- LeetCode 63: Unique Paths II
- [leetcode 63] Unique Paths II
- [LeetCode 63]Unique Paths II
- leetcode || 63、Unique Paths II
- Unique Paths II - LeetCode 63
- 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
- LeetCode 63 Unique Paths II
- LeetCode -- 63 Unique Paths II
- Linux 0.11 字符设备的使用
- Java this关键字详解
- 设计模式之见解三
- ubuntu16.04 安装 gem install rails报错解决
- php开发环境安装和服务器环境的部署
- leetcode 63 C++. Unique Paths II
- Java 之 大小数的加减乘除
- 有向图的十字链表存储算法(102)
- WebSocket基本
- 提高mysql千万级大数据SQL查询优化30条经验
- html基础学习
- minicom跨平台编译
- 阿里java面试全程经验
- 对象的原型与继承(一)