Error Index out of bound direction == 1 matrix[i][N] = sum_


#1

class Solution:
# @param A : integer
# @return a list of list of integers
def generateMatrix(self, A):
direction = 0
M = 0
matrix = [[i*0 for i in range(A)]]
N = A-1
R = 0
L = A-1
sum_ = 0

    while M<=N and R<=L:
        if direction == 0:
            for i in range(M,N+1):
                matrix[R][i] = sum_
                sum_ += 1
            R += 1  
            
        if direction == 1:     
            for i in range(R,L+1):
                matrix[i][N] = sum_
                sum_ += 1
            N -= 1  
        
        if direction == 2:
            for i in range(N,M-1,-1):
                matrix[L][i] = sum_
                sum_ += 1
            L -=1
        
        if direction == 3:
            for i in range(L,R-1.-1):
                matrix[i][L] = sum_
                sum_ += 1
            M += 1
        
        direction = (direction + 1)%4
        
    return matrix