# Easy to understand python 48ms

• It is the same idea with the following solution, except that now left is equal to up and right is equal to bottom since the matrix is N*N.
https://leetcode.com/discuss/34725/easy-to-understanding-python-44ms

``````class Solution:
# @param {integer} n
# @return {integer[][]}
def generateMatrix(self, n):
if n == 0:
#return [[]]
return []
if n == 1:
return [[1]]
matrix = [[0 for x in range(n)] for x in range(n)]
#left = up = 0 and right = bottom = n - 1
left = 0
right = n - 1
cur = 1
while left < right:
for i in xrange(left, right+1):
matrix[left][i] = cur
cur += 1
for i in xrange(left+1, right+1):
matrix[i][right] = cur
cur += 1
for i in reversed(xrange(left, right)):
matrix[right][i] = cur
cur += 1
for i in reversed(xrange(left+1, right)):
matrix[i][left] = cur
cur += 1
right -= 1;
left += 1
#Skip one last loop when left == right to save time
if n % 2 == 1:
matrix[left][left] = cur
return matrix``````

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