Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 独特路径2_Python 3.x - Fatal编程技术网

Python 3.x 独特路径2

Python 3.x 独特路径2,python-3.x,Python 3.x,机器人位于m x n网格的左上角 机器人只能在任何时间点向下或向右移动。机器人正试图到达网格的右下角 class Solution: """ @param obstacleGrid: A list of lists of integers @return: An integer """ def uniquePathsWithObstacles(self, obstacleGrid): # write your code here

机器人位于m x n网格的左上角

机器人只能在任何时间点向下或向右移动。机器人正试图到达网格的右下角

class Solution:
    """
    @param obstacleGrid: A list of lists of integers
    @return: An integer
    """
    def uniquePathsWithObstacles(self, obstacleGrid):
        # write your code here
        if not obstacleGrid:
            return 0

        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        li = [[0] * n] * m

        for i in range(m):
            for j in range(n):
                if obstacleGrid[i][j] == 1:
                    li[i][j] = 0 ######## why do I have to add this line ???########
                    continue
                elif i == 0 and j == 0:
                    li[i][j] = 1
                elif i == 0:
                    li[i][j] = li[i][j - 1]
                elif j == 0:
                    li[i][j] = li[i - 1][j]
                else:
                    li[i][j] = li[i - 1][j] + li[i][j - 1]

        return li[m - 1][n - 1]
如果网格中添加了一些障碍物。会有多少条独特的路径

障碍物和空白空间在网格中分别标记为1和0

class Solution:
    """
    @param obstacleGrid: A list of lists of integers
    @return: An integer
    """
    def uniquePathsWithObstacles(self, obstacleGrid):
        # write your code here
        if not obstacleGrid:
            return 0

        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        li = [[0] * n] * m

        for i in range(m):
            for j in range(n):
                if obstacleGrid[i][j] == 1:
                    li[i][j] = 0 ######## why do I have to add this line ???########
                    continue
                elif i == 0 and j == 0:
                    li[i][j] = 1
                elif i == 0:
                    li[i][j] = li[i][j - 1]
                elif j == 0:
                    li[i][j] = li[i - 1][j]
                else:
                    li[i][j] = li[i - 1][j] + li[i][j - 1]

        return li[m - 1][n - 1]

问题在于编码。我已经设置了用零填充的m*n矩阵。为什么我要再次将零指定给该位置???如果我删除那条线,似乎就行不通了。有人能告诉我原因吗???谢谢

问题在于这一行:

li = [[0] * n] * m
语法
[a]*n
创建
a
的浅拷贝,而不是深拷贝

例如:

n = m = 2
li[0][0] = 3
print(li)  # prints [[3, 0], [3, 0]]

讨论可能的解决办法

请在你说“如果我删除那条线,它似乎就不起作用了”的地方写上这句话。它要么在删除该行时起作用,要么不起作用。在第一种情况下,你已经证明了这一行是没有必要的,你的问题的答案可以归结为“有时程序员会写一些行来向读者证明已经是正确的。”在后一种情况下,你可以重新表述你的标题,使其更有意义,例如,“为什么我需要将一个已经设置为0的值设置为0?”当然,在编辑时,通过在每行代码前面放置4个空格来修正格式。你可能想考虑用一个问题来代替一个任务转储。这些通常会吓跑想回答的人。谢谢你的推荐。我得到了它!非常感谢!