Solution working on my machine but not able to submit on leetcode


  • 0
    B
        def numIslands(self, grid):
            """
            :type grid: List[List[str]]
            :rtype: int
            """
            visited = [[False]*len(grid[0]) for _ in range(len(grid))]
            count = 0
    
            for i in range(len(grid)):
                for j in range(len(grid[0])):
                    if grid[i][j] == 1 and not visited[i][j]:
                        self.runDFS(grid,i,j,visited)
                        count += 1
    
            return count
    
        def runDFS(self,grid,i,j,visited):
            visited[i][j] = True
            r = [0,0,1,-1]
            c = [1,-1,0,0]
            for p in range(len(r)):
                if self.isNeighbour(grid,i+r[p],j+c[p],visited):
                    self.runDFS(grid,i+r[p],j+c[p],visited)
    
    
        def isNeighbour(self,grid,i,j,visited):
            if i>=0 and j>=0 and i<len(grid) and j<len(grid[0]) and grid[i][j] == 1 and not visited[i][j]:
                return True
            else:
                return False
    
    LeetCode interpreter is always returning 0 for this code whereas it works fine when run on my local machine. Any idea as to what might be wrong here ?

Log in to reply
 

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