-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0059__spiral_matrix_2.py
61 lines (49 loc) · 1.52 KB
/
0059__spiral_matrix_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
LeetCode: https://leetcode.com/problems/spiral-matrix-ii/
"""
from typing import List
from unittest import TestCase
class Solution(TestCase):
def test_example_1(self):
expected = [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
self.assertEqual(expected, self.generateMatrix(3))
def test_example_2(self):
self.assertEqual([[1]], self.generateMatrix(1))
def generateMatrix(self, n: int) -> List[List[int]]:
matrix = [[0] * n for _ in range(n)]
ptr = 1
left, right, top, bottom = 0, n - 1, 0, n - 1
while ptr <= n ** 2:
# left -> right
row = top
for col in range(left, right + 1):
matrix[row][col] = ptr
ptr += 1
top += 1
if ptr > n ** 2:
break
# top -> down
col = right
for row in range(top, bottom + 1):
matrix[row][col] = ptr
ptr += 1
right -= 1
if ptr > n ** 2:
break
# right -> left
row = bottom
for col in range(right, left - 1, -1):
matrix[row][col] = ptr
ptr += 1
bottom -= 1
if ptr > n ** 2:
break
# bottom -> top
col = left
for row in range(bottom, top - 1, -1):
matrix[row][col] = ptr
ptr += 1
left += 1
if ptr > n ** 2:
break
return matrix