【Leet Code】59. Spiral Matrix II---Medium

来源:互联网 发布:pycharm与python 编辑:程序博客网 时间:2024/06/06 18:24

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 ]]

思路1:

按照题目54的方法按照左-->右,上-->下,右-->左,下-->上的顺序将元素存入数组中。

思路2:

相对于题目54,该题目有一个明显的特征:行=列。

//思路1代码实现:class Solution {public:    vector<vector<int>> generateMatrix(int n) {        vector<vector<int>> results(n,vector<int>(n));                    // put the element        int sx = 0, sy = 0, ex = n, ey = n;        int coun = 1;        while(sx < ex || sy < ey)        {            // left -> right            if(sy < ey)            {                for(int i = sy, j = sx; j < ex; ++j)                {                    results[i][j]= coun;                    ++coun;                }                ++sy;            }                        // up -> down            if(sx < ex)            {                for(int i = sy, j = ex-1; i < ey; ++i)                {                    results[i][j] = coun<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">;</span>                    ++coun;                }                --ex;             }                        // right -> left            if(sy < ey)            {                for(int i = ey - 1, j = ex-1; j >= sx; --j)                {                    results[i][j] = coun;                    ++coun;                }                --ey;             }                        // down -> up            if(sx < ex)            {                for(int i = ey - 1, j = sx; i >= sy; --i)                {                    results[i][j] = coun;                    ++coun;                }                ++sx;             }        }                return results;    }};

//思路2:            vector<vector<int>> matrix(n, vector<int>(n, 0));            int t = 0, num = 1;            while( t < n ) {                for( int i=t; i<n-t; i++ ) matrix[t][i] = num++;                for( int i=t+1; i<n-t; i++ ) matrix[i][n-t-1] = num++;                for( int i=n-t-2; i>=t; i-- ) matrix[n-t-1][i] = num++;                for( int i=n-t-2; i>=t+1; i-- ) matrix[i][t] = num++;                t++;            }            return matrix;

//思路2的另一种巧妙实现:class Solution {public:    vector<vector<int>> generateMatrix(int n) {        vector<vector<int> > mat(n, vector<int>(n));        int r = 0, c = -1, x = 1;        const int g[5] = { 0, 1, 0, -1, 0 };        for (int b = 0, i = 0; n > 0; n -= (b ^= 1), i = (++i % 4)) {            for (int s = 0; s < n; s++) {                mat[r += g[i]][c += g[i+1]] = x++;            }        }        return mat;    }};

巧妙实现方法的解答:
the outer loop produces the step size of each side e.g., for 5x5 , it'll be 5,4,4,3,2,2,1 notice the decrements are alternating between 1 and 0, which is what b^=1 produce the outer loop also produces a state variable i, representing which side it's filling the number for, and whose value can take on 0,1,2,3, g[5] stores the row/column index increment for each side, notice the increment for row and column index follows a similar pattern, except row lead column index by 1/4 cycle. finnaly the inner loop just go thru 1 side and fill in the #s with correct index


0 0