Is there a bug in the python version while testing ?


  • 0
    V

    When I submit my program I get an error but when I use the same input that is used for testing in custom test case my program works fine. Is there a buy in the testing machine ?This is my version of the code:

    visited = set([])
        class Solution(object):
            def numIslands(self, grid):
                """
                :type grid: List[List[str]]
                :rtype: int
                """
                if len( grid ) == 0:
                    return 0
                global count
                m = len( grid )
                n = len( grid[0] )
                #print m 
                #print n
                rvec = [-1,0,1,0]
                cvec = [ 0,1,0,-1]
                count = 0
                for i in range(m):
                    for j in range(n):
                        if grid[i][j] == '0':
                            continue
                        count += self.recurse( grid, m , n , rvec, cvec, i , j )
                return count
                
            def recurse( self, grid, m , n,rvec, cvec, i, j):
                global visited
                if str(i) + str(j) in visited:
                    return 0
                visited.add( str(i) + str(j) )
                for r,c in map( None, rvec, cvec ):
                    nx = r + i
                    ny = c + j
                    if nx >= 0 and nx < m and ny >= 0 and ny < n and ( str(nx) + str(ny) ) not in visited and grid[nx][ny] != '0':
                        self.recurse( grid, m ,n ,rvec, cvec, nx, ny )
                return 1

Log in to reply
 

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