Leetcode 59. Spiral matrix II (Spiral Matrix II)

This is my first blog, first casually write, follow-up will add more content.

Note: This indicates the position of the array (index) are to start with an index 0.

Subject description:

Given a positive integer  n- , containing 1 to generate a  n- 2 all of the elements, and the element arranged spirally clockwise order square matrix.

solution:

This question is the focus of the relationship is to find an iterative array corresponding position (index), observe the following example:

I represents a row, j represents a column, n represents the number of (the number of columns) of rows

[ 
 [1, 2, 3, 4], 
 [12, 13, 14, 5], 
 [11, 16, 15, 6], 
[10, 9, 8, 7],
]

from the first line of the first position (0, 0) to (0, 3), consistent with the position of the respective number of rows, the number of columns is increasing;
when the first row is filled, the remaining figures will last from a first position (0, 3) begins to store digital down until it reaches a final position (3, 3).
In this process, the number of columns is unchanged, the number of lines has been increased, until it reaches position (3, 3); Next Similarly, from the position (3, 3) to a position (3, 0),
the number of lines is not while reducing the number of columns in the variable; in the final position (3, 3) to a position (1, 0), reducing the number of columns and rows invariant.
Can be found in an iterative process 1-12 and 13-16 iterative process is similar, are from the beginning has been to the bottom of the iterative initial position.
[
 [  1,  2,  3,  4 ],
 [ 12,           5 ],
 [ 11,           6 ],
[ 10, 9, 8, 7 ],
]
[ 
 [13, 14], 
 [16, 15], 
]

the four critical points change occurs 1-12 iteration find (represented by algebraic):
(0,. 1-n-), (. 1-n-, n- -1), (n--. 1, 0), (. 1, 0)
13-16 iterative critical point:
(. 1, n--2), (n--2, n--2), (n--2,. 1) , (2, 1)
is easy to find the following relationship:
H = 0;
(0, NH-1), (NH-1, NH-1), (NH-1, 0), (H + 1, H)
when arrival point (h + 1, h) time, h ++ (due to internal start from a new round of iteration), and i, j incremental mode should be restored to its original state, that is the same i, j is incremented.
This can occur later in the code easily, and only need to generate iterations n * n digits are stored in the array location corresponding to it.
Here is the code implemented in Python:

def generateMatrix(n):
    res = [[0 for i in range(n)] for i in range(n)]
    i, j, flag = 0, 0, 0
    h = 0
    for k in range(1, n*n+1):
        if j == n-h-1 and i == h: 
            flag = 1
        elif i == n-h-1 and j == n-h-1:
            flag = 2
        elif i == n-h-1 and j == h:
            flag = 3
        elif i == h+1 and j == h:
            h += 1
            flag = 0
        res[i][j] = k
        if flag == 0: 
            j += 1
        elif flag == 1:
            i += 1
        elif flag == 2:
            j -= 1
        elif flag == 3:
            i -= 1
    return res

 

Guess you like

Origin www.cnblogs.com/Alevals/p/11563398.html