Python stack limit exceeded?


  • 0
    S

    I'm getting stack limit exceeded on one of the test cases which is a 100x100 matrix of all 1's.

    Anyone have an idea what is wrong with my implementation?

     class Solution(object):
    
        def findCircleNum(self, A):
            
            count = 0
            
            def dfs(row, column):
                if row < 0 or column >= len(A[0]) or row >= len(A) or column < 0:
                    return
                
                if A[row][column] == 0:
                    return
                
                A[row][column] = 0
                
                for i in range(row-1, row+2):
                    if i >= 0 and i<len(A) and A[i][column] == 1:
                        dfs(i, column)
                for i in range(column-1, column+2):
                    if i >= 0 and i<len(A[0]) and A[row][i] == 1:
                        dfs(row, i)
                    
                
            
            
            for row in range(len(A)):
                for column in range(len(A[0])):
                    if A[row][column] == 1:
                        dfs(row, column)
                        count += 1
                        
            
            return count
    

Log in to reply
 

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