MCPcopy
hub / github.com/HuberTRoy/leetCode / generateMatrix

Method generateMatrix

Array/SpiralMatrixII.py:25–115  ·  view source on GitHub ↗

:type n: int :rtype: List[List[int]]

(self, n)

Source from the content-addressed store, hash-verified

23class Solution(object):
24
25 def generateMatrix(self, n):
26 """
27 :type n: int
28 :rtype: List[List[int]]
29 """
30
31 maps = [[0 for i in range(n)] for j in range(n)]
32
33 current_value = [i for i in range(1, n*n+1)]
34 current_value.reverse()
35
36 def makeXY(x, y):
37 # up
38 # down
39 # right
40 # left
41 return [(x, y-1),
42 (x, y+1),
43 (x+1, y),
44 (x-1, y)]
45
46 def right(x, y):
47
48 while 1:
49 if not current_value:
50 return maps
51 xy = makeXY(x, y)
52 if (y > -1 and x > -1) and (y < n and x < n):
53 if maps[y][x] == 0:
54 maps[y][x] = current_value.pop()
55 y, x = xy[2][1], xy[2][0]
56 else:
57 # down
58 return down(x-1, y+1)
59 else:
60 # down
61 return down(x-1, y+1)
62
63 def down(x, y):
64
65 while 1:
66 if not current_value:
67 return maps
68 xy = makeXY(x, y)
69 if (y > -1 and x > -1) and (y < n and x < n):
70 if maps[y][x] == 0:
71 maps[y][x] = current_value.pop()
72 y, x = xy[1][1], xy[1][0]
73
74 else:
75 # left
76 return left(x-1, y-1)
77 else:
78 # left
79 return left(x-1, y-1)
80 def left(x, y):
81
82 while 1:

Callers

nothing calls this directly

Calls 1

reverseMethod · 0.80

Tested by

no test coverage detected