Spiral Matrix II
Spiral Matrix
Given an integer n, generate a square matrix filled with elements from 1 to n2 in Spiral order.
For example,
Given N = 3
,
You should return the following matrix:
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
It's not easy. Look at the blog Park someone interviewed encountered this problem, long-winded to see me dizzy. Run to the Leetcode discussion area, it seems to see or I Python Dafa, four lines will be done you believe.
| 1 | 2 | 3 |
| 8 | 9 | 4 |
| 7 6 | 5 |
If you don't see it, the general process is like this, and the same color means it's executed in the same step.
>>> def generatematrix (n): ... A, low = [], n*n+1 ... While low > 1: ... Low, High = Low-len (A), low ... A = [List (range low, high)] + list (Zip (*a[::-1)) # python3.3 syntax differs from 2.7 ... Print (A) ... Return a...>>> Generatematrix (1) [[]][[1]][[1]]>>> Generatematrix (2) [[]][[4]][[3], (4,)][[1, 2], (4, 3)][[1, 2], (4, 3)]>>> Generatematrix (3) [[]][[9]][[8], (9,)][[6, 7], (9, 8)][[4, 5], (9, 6), (8, 7)] [[1, 2, 3], (8, 9, 4), (7, 6, 5)][[1, 2, 3], (8, 9, 4), (7, 6, 5)]
Leetcode-spiral matrix II Spiral Matrix 2 python Dafa good, four lines will be done, you believe?