Python straight forward solution


  • 1
    J
    class Solution(object):
        def generateMatrix(self, n):
            """
            :type n: int
            :rtype: List[List[int]]
            """
            res = [[1]*n for _ in xrange(n)]
            a,b,c,d = 0, n-1, n-1, 0
            p = 0
            q = n
            k = 1
            while k < n*n + 1:
                for i in xrange(p,q):
                    res[a][i] = k
                    k += 1            
                a += 1
                for i in xrange(p+1,q):
                    res[i][b] = k
                    k += 1
                b -= 1
                for i in xrange(q-2, p-1,-1):
                    res[c][i] = k
                    k += 1
                c -= 1
                for i in xrange(q-2, p, -1):
                    res[i][d] = k
                    k += 1
                d += 1
                p += 1
                q -= 1
    
            return res

Log in to reply
 

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