Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
themeEmacs
titleSpiral Matrix II
linenumberstrue
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        map = [[0] * n for i in range(n)]
        count = 0
        cur = (0, 0)

        while count < n * n:
            count += 1
            
            x = cur[0]
            y = cur[1]
            map[y][x] = count
            
            # left, up, right, down
            if x > 0 bottom_block = y == n-1 or (y < n-1 and map[y+1][x-1] !== 0)
and y == n-1:         
       x -= 1   # left, up, right, down
     elif        if x > 0 and map[y][x-1] == 0 and y < n-1 and map[y+1][x] != 0bottom_block:
                x -= 1
            elif y > 0 and map[y-1][x] == 0:
                y -= 1
            elif x < n-1 and map[y][x+1] == 0:
                x += 1
            elif y < n-1 and map[y+1][x] == 0:
                y += 1

              

            cur = (x, y)
            
        return map