<filename>python/Array/59_spiral_matrix_ii.py<gh_stars>0
# !/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Description:
    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
    For example,
    Given n = 3,
    You should return the following matrix:
    [
     [ 1, 2, 3 ],
     [ 8, 9, 4 ],
     [ 7, 6, 5 ]
    ]

Tags: Array
Time: O(n^2); Space: O(1)
'''

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        matrix = [[0 for _ in xrange(n)] for _ in xrange(n)]
        left, right, top, bottom, num = 0, n - 1, 0, n - 1, 1
        while left <= right and top <= bottom:
            for j in xrange(left, right+1):
                matrix[top][j] = num
                num += 1
            for i in xrange(top+1, bottom):
                matrix[i][right] = num
                num += 1
            for j in reversed(xrange(left, right+1)):
                if top < bottom:
                    matrix[bottom][j] = num
                    num += 1
            for i in reversed(xrange(top+1, bottom)):
                if left < right:
                    matrix[i][left] = num
                    num += 1
            left, right, top, bottom = left+1, right-1, top+1, bottom-1
        return matrix

<filename>python/Array/59_spiral_matrix_ii.py<gh_stars>0
# !/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Description:
    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
    For example,
    Given n = 3,
    You should return the following matrix:
    [
     [ 1, 2, 3 ],
     [ 8, 9, 4 ],
     [ 7, 6, 5 ]
    ]

Tags: Array
Time: O(n^2); Space: O(1)
'''

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        matrix = [[0 for _ in xrange(n)] for _ in xrange(n)]
        left, right, top, bottom, num = 0, n - 1, 0, n - 1, 1
        while left <= right and top <= bottom:
            for j in xrange(left, right+1):
                matrix[top][j] = num
                num += 1
            for i in xrange(top+1, bottom):
                matrix[i][right] = num
                num += 1
            for j in reversed(xrange(left, right+1)):
                if top < bottom:
                    matrix[bottom][j] = num
                    num += 1
            for i in reversed(xrange(top+1, bottom)):
                if left < right:
                    matrix[i][left] = num
                    num += 1
            left, right, top, bottom = left+1, right-1, top+1, bottom-1
        return matrix

# !/usr/bin/env python # -*- coding: utf-8 -*- Description:     Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.     For example,     Given n = 3,     You should return the following matrix:     [      [ 1, 2, 3 ],      [ 8, 9, 4 ],      [ 7, 6, 5 ]     ]  Tags: Array Time: O(n^2); Space: O(1) :type n: int         :rtype: List[List[int]]