54. Spiral Matrix

Viewed 31

I have attempted the leetcode problem and it works almost till the end. The up function is not successfully calling the right function. Finding it hard to figure out the reason. There might be better ways to solve this problem but would like to know what is the bug in my code. Please help find it. Thanks in Advance

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
    rows=len(matrix)
    cols=len(matrix[0])
    l=[]
    def right(rows,cols):
        for r in range(rows):
            for c in range(cols):
                if matrix[r][c]=="X":
                    return l
                l.append(matrix[r][c])
                matrix[r][c]="X"
                if c+1==rows:
                    down(rows,cols,c)
                    
    def down(rows,cols,f):
        for r in (range(1,rows)):
            l.append(matrix[r][f])
            matrix[r][f]="X"
            if r+1==rows:
                left(rows-1,cols-1)
    
    
    def left(rows,cols):

        for c in range(cols-1,-1,-1):
            l.append(matrix[rows][c])
            matrix[rows][c]="X"
            if c-1==-1:
                up(rows-1,cols,c)
           
            
        '''up(rows-1,cols-1,c)'''
        
    def up(rows,cols,f):
        for r in (range(rows,0,-1)):
            l.append(matrix[r][f])
            matrix[r][f]="X"
            if matrix[r-1][f]=="X": 
                print(matrix[r-1][f])
                right(rows,cols+1)
                
    right(rows,cols)
    return l

TestCase: [[1,2,3],[4,5,6],[7,8,9]]

0 Answers
Related