a piece of Python welcome advices


  • 1
        res = [[] for i in range(n)]
        for i in range(n):
            res[i] = [0]*n
            
        base = 0
        
        for circle in range(n/2):
            for i in range(n-1-circle*2):
                res[circle][i+circle] = base+1+i                                # do the up
                res[i+circle][n-1-circle] = base+1+(n-1-circle*2)*1+i           # do the right
                res[n-1-circle][n-1-circle-i] = base+1+(n-1-circle*2)*2+i       # do the bottom
                res[n-1-circle-i][circle] = base+1+(n-1-circle*2)*3+i     # do the left
            base += 4*(n-1-circle*2)
            
        # even or odd
        if n%2==1:
            res[n/2][n/2] = n*n
        return res

Log in to reply
 

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