LeetCode刷题笔记--63. Unique Paths II

63. Unique Paths II

Medium

816104FavoriteShare

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

这道题和62差不多,不同的是对障碍物的处理,首行和首列如果遇到障碍物,后面就都是0了。然后从[1][1]开始,如果非1,则等于左边加上面,如果等于1则置0.

下面的代码居然溢出了,溢出了...

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if(obstacleGrid.size()==0||obstacleGrid.size()==1)return obstacleGrid.size();
        for(int y=0;y<obstacleGrid[0].size();y++)
        {
            if(obstacleGrid[0][y]==0)obstacleGrid[0][y]=1;
            else
            {
                obstacleGrid[0][y]=0;
                y++;
                while(y<obstacleGrid[0].size())
                {
                    obstacleGrid[0][y]=0;
                    y++;
                }
                break;
            }
        }
        for(int x=1;x<obstacleGrid.size();x++)
        {
            if(obstacleGrid[x][0]==0)obstacleGrid[x][0]=1;
            else
            {
                obstacleGrid[x][0]=0;
                x++;
                while(x<obstacleGrid.size())
                {
                    obstacleGrid[x][0]=0;
                    x++;
                }
                break;
            }
        }
       // return obstacleGrid[0][1];
        for(int x=1;x<obstacleGrid.size();x++)
        {
            for(int y=1;y<obstacleGrid[0].size();y++)
            {
                if(obstacleGrid[x][y]==0)
                {
                    obstacleGrid[x][y]=obstacleGrid[x-1][y]+obstacleGrid[x][y-1];
                }
                else
                {
                    obstacleGrid[x][y]=0;
                }
                
            }
        }
        return obstacleGrid[obstacleGrid.size()-1][obstacleGrid[0].size()-1];
        
    }
};

报错信息如下:

runtime error: signed integer overflow: 1053165744 + 1579748616 cannot be represented in type 'int' (solution.cpp)这说明什么??说明int装不下了。难道要用long?看到网上没人用long啊,为什么我的就不行??改一下代码看看。

果然重新弄了个数组ans,改成long以后就好了,注意[[1]],[[0]]这两个testcase。

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if(obstacleGrid.size()==0)return obstacleGrid.size();
        int rows=obstacleGrid.size();
        int cols=obstacleGrid[0].size();
        if(obstacleGrid[0][0]==1||obstacleGrid[rows-1][cols-1]==1)return 0;
        vector<vector<long>> ans(rows,vector<long>(cols,0));
        for(int y=0;y<cols;y++)
        {
            if(obstacleGrid[0][y]==0)ans[0][y]=1;
            else break;
        }
        for(int x=1;x<rows;x++)
        {
            if(obstacleGrid[x][0]==0)ans[x][0]=1;
            else break;
        }
       // return obstacleGrid[0][1];
        for(int x=1;x<rows;x++)
        {
            for(int y=1;y<cols;y++)
            {
                if(obstacleGrid[x][y]==0)
                {
                    ans[x][y]=ans[x-1][y]+ans[x][y-1];
                }
                else
                {
                    ans[x][y]=0;
                }
                
            }
        }
        return ans[rows-1][cols-1];
        
    }
};

猜你喜欢

转载自blog.csdn.net/vivian0239/article/details/89709216