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

这道题目与62题类似,但需处理障碍物。机器人从网格左上角开始,只能向下或向右移动,目标是到达右下角。障碍物标记为1,空格为0。当遇到障碍物时,路径数为0。算法从[1][1]开始,若当前位置为0,则路径数等于左方和上方的和;若为1,则路径数为0。代码中要注意整型溢出,可以使用长整型避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];
        
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值