NC325. 不同路径的数目(二)
描述
起点(1) | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 终点(1) |
示例1
输入:
[[1,1,1],[1,0,1],[1,1,1]]
输出:
2
说明:
从左上角到右下角一共有2条不同的路径:示例2
输入:
[[1,0,1]]
输出:
0
说明:
从左上角到右下角没有路径可以到达C++ 解法, 执行用时: 4ms, 内存消耗: 420KB, 提交时间: 2022-06-17
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param obstacleGrid int整型vector<vector<>> * @return int整型 */ int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { // write code here if(obstacleGrid.size() == 0 || obstacleGrid[0].size() == 0){ return 0; } vector<int> dp = obstacleGrid[0]; for(int i = 1; i < obstacleGrid.size(); i++){ int pv = obstacleGrid[i][0]; int accum = pv; for(int j = 1; j < obstacleGrid[0].size(); j++){ dp[j] = obstacleGrid[i][j] * (pv + dp[j]); pv = dp[j]; accum += pv; } if (accum == 0){ return 0; } } return dp.back(); } };
C++ 解法, 执行用时: 4ms, 内存消耗: 512KB, 提交时间: 2022-04-02
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param obstacleGrid int整型vector<vector<>> * @return int整型 */ int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { // write code here int size_row =obstacleGrid.size(); int size_col = obstacleGrid[0].size(); vector< vector<int> > dp(size_row+1,vector<int>(size_col+1,0)); for(int i=0;i<size_row;i++) { for(int j = 0; j< size_col; j++) { //dp[i][j] =dp[i-1][j]+dp[i][j-1]; if((i == 0 || j==0) && obstacleGrid[i][j] ==1) dp[i][j] =1; else { if(obstacleGrid[i][j] ==1 ) dp[i][j] = dp[i-1][j]+dp[i][j-1]; } } } return dp[size_row-1][size_col-1]; } };
C++ 解法, 执行用时: 4ms, 内存消耗: 520KB, 提交时间: 2022-03-04
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param obstacleGrid int整型vector<vector<>> * @return int整型 */ int uniquePathsWithObstacles(vector<vector<int> >& obs) { // write code here int m = obs.size(); int n = obs[0].size(); vector<vector<int> > route(m,vector<int>(n,1)); route[0][0] = obs[0][0]==1?1:0; for(int i = 1;i<m;++i){ for(int j = 1;j<n;++j){ if(obs[i][j - 1] == 1 && obs[i-1][j] == 1){ route[i][j]=route[i][j-1] + route[i-1][j]; } else if(obs[i][j - 1] == 0 && obs[i-1][j] == 1){ route[i][j] = route[i-1][j]; } else if(obs[i][j - 1] == 1 && obs[i-1][j] == 0){ route[i][j] = route[i][j-1]; } else { route[i][j] = 0; } } } return route[m-1][n-1]; } };
C++ 解法, 执行用时: 4ms, 内存消耗: 524KB, 提交时间: 2022-07-20
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param obstacleGrid int整型vector<vector<>> * @return int整型 */ int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { // write code here int m = obstacleGrid.size(), n = obstacleGrid[0].size(); vector<int> dp(n, 0); for (int i = 0; i < n; i++) { if (obstacleGrid[0][i] == 0) { dp[i] = 0; } else if (i == 0) { dp[i] = 1; } else { dp[i] = dp[i - 1]; } } for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { if (obstacleGrid[i][j] == 0) { dp[j] = 0; } else if(j != 0){ dp[j] += dp[j - 1]; } } } return dp[n - 1]; } };
C++ 解法, 执行用时: 4ms, 内存消耗: 524KB, 提交时间: 2022-06-22
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { if (obstacleGrid.size() == 0 || obstacleGrid[0].size() == 0) {return 0;} vector<int> a = obstacleGrid[0]; for (int i = 1; i < obstacleGrid.size(); i++) { int b = obstacleGrid[i][0]; int c = b; for (int j = 1; j < obstacleGrid[0].size(); j++) { a[j] = obstacleGrid[i][j] * (b + a[j]); b = a[j]; c += b; } if (c == 0) {return 0;} } return a.back(); } };