Python with DFS


  • 0
    R
    class Solution(object):
        def floodFill(self, image, sr, sc, newColor):
            if image[sr][sc] == newColor:
                return image
           
            start_mark = image[sr][sc]
            r,c = len(image),len(image[0])
            
            def dfs(i,j):
                if 0<=i<r and 0<=j<c and image[i][j]==start_mark:
                    image[i][j] = newColor
                    dfs(i-1,j)
                    dfs(i,j-1)
                    dfs(i+1,j)
                    dfs(i,j+1)
                
            
            dfs(sr,sc)
            return image
    

Log in to reply
 

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