63. Unique Paths II -Medium

Question

*Follow up for “Unique Paths”:

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.

跟进”Unique Paths”,现在在表格中加一个障碍物(标记为1的是障碍物),有多少种路径可以到达终点。(m和n不会超过100)

Example

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

The total number of unique paths is 2.

Solution

  • 动态规划解。原理和原题一样,取左边和上面的方法之和即可,对于障碍物,我们只需把它设为dp[i] = 0即可,这样不会影响到后面的计算。额外要考虑的是起始点和终点会不会有障碍物(居然有这样的测试集。。)。

    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            if len(obstacleGrid) == 0 or len(obstacleGrid[0]) == 0: return 0
            # 如果起点或终点为1,则不可能有路径
            if obstacleGrid[0][0] == 1 or obstacleGrid[-1][-1] == 1: return 0
    
            dp = [[0 for _ in range(len(obstacleGrid[0]))] for _ in range(len(obstacleGrid))]
            dp[0][0] = 1
            for index_r in range(len(obstacleGrid)):
                for index_c in range(len(obstacleGrid[0])):
                    if index_c == 0 and index_r == 0: continue
                    # 如果该表格是障碍,跳过
                    if obstacleGrid[index_r][index_c] == 1: continue
                    # 第一行的来源只能为左边(障碍物表格的dp为0,所以其右边也都是0)
                    if index_r == 0:
                        dp[index_r][index_c] = dp[index_r][index_c - 1]
                    # 第一行的来源只能为上面(障碍物表格的dp为0,所以其下边也都是0)
                    elif index_c == 0:
                        dp[index_r][index_c] = dp[index_r - 1][index_c]
                    # 其他都是来自上面或左边(因为障碍的dp为0,所以它不影响结果)
                    else:
                        dp[index_r][index_c] = dp[index_r - 1][index_c] + dp[index_r][index_c - 1]
            return dp[-1][-1]
D:\桌面\OJ项目\douoj>npm run generate-api > douoj@0.1.0 generate-api > openapi-generator-cli generate -i http://localhost:8121/api/v2/api-docs -g typescript-axios -o ./generated --additional-properties=supportsES6=true Picked up JAVA_TOOL_OPTIONS: -Dstdout.encoding=UTF-8 -Dstderr.encoding=UTF-8 Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI). | Error count: 50, Warning count: 0 Errors: -attribute paths.'/api/post/list/page/vo'(post).responsesObject is unexpected -attribute paths.'/api/user/get'(get).responsesObject is unexpected -attribute paths.'/api/post/list/page'(post).responsesObject is unexpected -attribute paths.'/api/post/edit'(post).[postEditRequest].originalRef is unexpected -attribute paths.'/api/user/update'(post).responsesObject is unexpected -attribute paths.'/api/post_favour/'(post).responsesObject is unexpected -attribute paths.'/api/user/list/page/vo'(post).[userQueryRequest].originalRef is unexpected -attribute paths.'/api/user/register'(post).responsesObject is unexpected -attribute paths.'/api/post_favour/list/page'(post).responsesObject is unexpected -attribute paths.'/api/post/add'(post).responsesObject is unexpected -attribute paths.'/api/post/edit'(post).responsesObject is unexpected -attribute paths.'/api/post_favour/my/list/page'(post).responsesObject is unexpected -attribute paths.'/api/user/update/my'(post).[userUpdateMyRequest].originalRef is unexpected -attribute paths.'/api/post_favour/my/list/page'(post).[postQueryRequest].originalRef is unexpected -attribute paths.'/api/post/update'(post).responsesObject is unexpected -attribute paths.'/api/post_thumb/'(post).responsesObject is unexpected -attribute paths.'/api/post_favour/'(post).[postFavourAddRequest].originalRef is unexpected -attribute paths.'/api/user/login'(post).responsesObject is unexpected -attribute paths.'/api/user/add'(post).responsesObject is unexpected -attribute paths.'/api/post/list/page/vo'(post).[postQueryRequest].originalRef is unexpected -attribute paths.'/api/user/list/page'(post).responsesObject is unexpected -attribute paths.'/api/user/delete'(post).responsesObject is unexpected -attribute paths.'/api/post/search/page/vo'(post).responsesObject is unexpected -attribute paths.'/api/'(post).responsesObject is unexpected -attribute paths.'/api/file/upload'(post).responsesObject is unexpected -attribute paths.'/api/post/list/page'(post).[postQueryRequest].originalRef is unexpected -attribute paths.'/api/user/add'(post).[userAddRequest].originalRef is unexpected -attribute paths.'/api/post/my/list/page/vo'(post).[postQueryRequest].originalRef is unexpected -attribute paths.'/api/post/get/vo'(get).responsesObject is unexpected -attribute paths.'/api/user/login'(post).[userLoginRequest].originalRef is unexpected -attribute paths.'/api/post_thumb/'(post).[postThumbAddRequest].originalRef is unexpected -attribute paths.'/api/setMenu'(get).responsesObject is unexpected -attribute paths.'/api/user/login/wx_open'(get).responsesObject is unexpected -attribute paths.'/api/user/register'(post).[userRegisterRequest].originalRef is unexpected -attribute paths.'/api/user/update/my'(post).responsesObject is unexpected -attribute paths.'/api/post/add'(post).[postAddRequest].originalRef is unexpected -attribute paths.'/api/user/logout'(post).responsesObject is unexpected -attribute paths.'/api/post/update'(post).[postUpdateRequest].originalRef is unexpected -attribute paths.'/api/'(get).responsesObject is unexpected -attribute paths.'/api/user/list/page/vo'(post).responsesObject is unexpected -attribute paths.'/api/post/delete'(post).[deleteRequest].originalRef is unexpected -attribute paths.'/api/user/get/vo'(get).responsesObject is unexpected -attribute paths.'/api/user/get/login'(get).responsesObject is unexpected -attribute paths.'/api/post/search/page/vo'(post).[postQueryRequest].originalRef is unexpected -attribute paths.'/api/post/my/list/page/vo'(post).responsesObject is unexpected -attribute paths.'/api/post_favour/list/page'(post).[postFavourQueryRequest].originalRef is unexpected -attribute paths.'/api/user/delete'(post).[deleteRequest].originalRef is unexpected -attribute paths.'/api/post/delete'(post).responsesObject is unexpected -attribute paths.'/api/user/update'(post).[userUpdateRequest].originalRef is unexpected -attribute paths.'/api/user/list/page'(post).[userQueryRequest].originalRef is unexpected at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:717) at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:744) at org.openapitools.codegen.cmd.Generate.execute(Generate.java:527) at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
06-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值