leetcode刷题

目录

题目-机器人的路劲-递归+hashmap

寻找差值为定值的pair


题目-机器人的路劲-递归+hashmap

一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-paths-ii

思路

递归+hasnMap

代码

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        # 统计障碍物的位置
        r_max, c_max = len(obstacleGrid)-1, len(obstacleGrid[0])-1

        obstacle = set()
        for i in range(len(obstacleGrid)):
            for j in range(len(obstacleGrid[i])):
                if obstacleGrid[i][j] == 1:
                    obstacle.add(str(i) + "_" + str(j))
                    if i == r_max and j == c_max:
                        return 0

        countMap = {}

        # 递归函数
        def methodCount(i, j):
            if (i > r_max) or (j > c_max):
                return 0
            if (i == r_max) and (j == c_max):
                return 1

            key = str(i) + "_" + str(j)

            if key in obstacle:
                return 0
            if key not in countMap:
                res =  m
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值