-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
46 lines (38 loc) · 1.08 KB
/
sol.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
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
leftEnd = -1
righttEnd = len(matrix[0])
topEnd = -1
bottomEnd = len(matrix)
cur = (0, 0)
count = 0
needed = matrix * matrix[0]
spiral = []
movement = 'r'
def move(pos):
moves = {
"r": (0, 1),
"l": (0, -1),
"u": (-1, 0),
"d": (1, 0),
}
return (pos[0] + moves[pos][0], pos[1] + moves[pos][1])
def isInLimits(pos):
return (
leftEnd < pos[0] < righttEnd or
topEnd < pos[1] < bottomEnd
)
def getNext(cur):
nextPos = move(cur)
if isInLimits(nextPos):
return nextPos
def getVal(pos):
return matrix[pos(1)][pos[0]]
while count < needed:
spiral.append(getVal(cur))
count += 1
cur = getNext(cur)
x = (0,0)
y = (1,2)
print(x + y)