-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralMatrixIII.py
More file actions
26 lines (24 loc) · 817 Bytes
/
spiralMatrixIII.py
File metadata and controls
26 lines (24 loc) · 817 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/spiral-matrix-iii/
# Author: Miao Zhang
# Date: 2021-03-21
class Solution:
def spiralMatrixIII(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]:
res = []
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
d = 0 # east
k = 1
res.append([r0, c0])
if len(res) == R * C: return res
while k:
for i in range(2):
for j in range(k):
r0 += dirs[d][0]
c0 += dirs[d][1]
if r0 < 0 or r0 >= R or c0 < 0 or c0 >= C: continue
res.append([r0, c0])
if len(res) == R * C: return res
d = (d + 1) % 4;
k += 1
return res