python dfs, like number of islands


  • 0
    L
        def wallsAndGates(self, a):
            """
            :type rooms: List[List[int]]
            :rtype: void Do not return anything, modify rooms in-place instead.
            """
            for i in range(len(a)):
                for j in range(len(a[0])):
                    if a[i][j] == 0:
                        self.helper(i,j,a,0)
                           
        def helper(self,ii,jj,a,dist):
            if ii < 0 or jj < 0 or ii >= len(a) or jj >= len(a[0]) or a[ii][jj] < dist:
                    return
            a[ii][jj] = dist
            self.helper(ii + 1, jj, a,dist + 1)
            self.helper(ii - 1, jj, a,dist + 1)
            self.helper(ii, jj + 1, a,dist + 1)
            self.helper(ii, jj - 1, a,dist + 1)
    

Log in to reply
 

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