將一個正方形矩陣螺旋著填滿遞增的數字。
注意點:
無例子:
輸入: n = 3
輸出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
這道題跟 Spiral Matrix 正好相反,一個是螺旋著讀出數字,一個是螺旋著寫入數字,而且這道題還要簡單一點,因為形狀固定是正方形的,所以只要控制四條邊不斷向內縮進就可以了。考慮一下奇偶的情況,在奇數的時候要額外加一個中心點。
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
left = top = 0
right = n - 1
bottom = n - 1
num = 1
result = [[0 for __ in range(n)] for __ in range(n)]
while left < right and top < bottom:
for i in range(left, right):
result[top][i] = num
num += 1
for i in range(top, bottom):
result[i][right] = num
num += 1
for i in range(right, left, -1):
result[bottom][i] = num
num += 1
for i in range(bottom, top, -1):
result[i][left] = num
num += 1
left += 1
right -= 1
top += 1
bottom -= 1
if left == right and top == bottom:
result[top][left] = num
return result
if __name__ == "__main__":
assert Solution().generateMatrix(5) == [[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7],
[14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]