Why do I get a runtime error with inbuilt function vs separate function python?


  • 0
    L

    I get a runtime error with this code:

    class Solution(object):
        def wallsAndGates(self, rooms):
            """
            :type rooms: List[List[int]]
            :rtype: void Do not return anything, modify rooms in-place instead.
            """
            def dfs(i,j,d):
                for I,J in (i-1,j),(i+1,j),(i,j-1),(i,j+1):
                    if 0<=I<len(rooms) and 0<=J<len(rooms[0]) and d+1<rooms[I][J]:
                        rooms[I][J]=d+1
                        dfs(I,J,d+1)
            if not rooms: return
            m,n=len(rooms), len(rooms[0])
            for i in xrange(m):
                for j in xrange(n):
                    if not rooms[i][j]:
                        dfs(i,j,0)
    

    However, I do not get a runtime error with this (the only difference is the definition is moved outside)

    class Solution(object):
        def wallsAndGates(self, rooms):
            """
            :type rooms: List[List[int]]
            :rtype: void Do not return anything, modify rooms in-place instead.
            """
            
            for i in xrange(len(rooms)):
                for j in xrange(len(rooms[0])):
                    if rooms[i][j]==0: self.dfs(rooms,i,j)
        
        def dfs(self,rooms,i,j):
            for I,J in (i-1,j),(i+1,j),(i,j-1),(i,j+1):
                if 0<=I<len(rooms) and 0<=J<len(rooms[0]) and rooms[i][j]+1<rooms[I][J]:
                    rooms[I][J]=rooms[i][j]+1
                    self.dfs(rooms,I,J)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.