The TransCompilator

Solved LeetCode program · 59 of 1,000

#0059 · Spiral Matrix II

Medium Array chunk-02.md

Original English source

The size is 3. The matrix is a list of 9 zeros. The visited is a list of 9 zeros. The row is 0. The column is 0. The down is 0. The right is 1. The counter is 1.
For every step from 0 to 8.
Set item (row times size plus column) of matrix to counter. Set item (row times size plus column) of visited to 1. Add 1 to counter. The nextrow is row plus down. The nextcolumn is column plus right. The blocked is 0. The olddown is 0. If nextrow is less than 0 or nextrow is at least size or nextcolumn is less than 0 or nextcolumn is at least size, set blocked to 1. If blocked is 0 and item (nextrow times size plus nextcolumn) of visited is 1, set blocked to 1. If blocked is 1, set olddown to down. If blocked is 1, set down to right. If blocked is 1, set right to 0 minus olddown. Increase row by down. Increase column by right.

For each value in matrix, report value.

Open in Playground Browse all 1,000 problems

Expected stdout

1
2
3
8
9
4
7
6
5