Created
April 17, 2022 22:54
-
-
Save ThomasHigginson/96f17205764ff12f926a22b88518b599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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