Skip to content

Instantly share code, notes, and snippets.

@ThomasHigginson
Created April 17, 2022 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasHigginson/96f17205764ff12f926a22b88518b599 to your computer and use it in GitHub Desktop.
Save ThomasHigginson/96f17205764ff12f926a22b88518b599 to your computer and use it in GitHub Desktop.
def generateMatrix(self, n: int) -> List[List[int]]:
numLayers = (n+1)//2
ret = [[0]*n for x in range(0, n)]
cnt = 1
for layer in range(0, numLayers):
# Fill the top row of layer
for i in range(layer, n-layer):
ret[layer][i] = cnt
cnt += 1
# Fill right side of layer
for i in range(layer+1, n-layer):
ret[i][n-layer-1] = cnt
cnt += 1
# Fill bottom side
for i in range(n-layer-1-1, layer-1, -1):
ret[n-layer-1][i] = cnt
cnt += 1
# Fill left side
for i in range(n-layer-1-1, layer, -1):
ret[i][layer] = cnt
cnt += 1
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment