LeetCode | 59. Spiral Matrix II

82 篇文章 0 订阅
19 篇文章 0 订阅

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

题意:填充蛇形矩阵

class Solution {
public:
/*
    蛇形矩阵
*/
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int> > matrix;
        int temp = 0;
        for(int i=0;i<n;i++)
        {
            vector<int> line;
            for(int j=0;j<n;j++)
            {
                line.push_back(temp);
            }
            matrix.push_back(line);
        }

        vector<int> res;
        int x = 0, y = 0, tot = 1;  //tot用于计数,作为停止条件
        int up = 0, down = n-1, left = 0, right = n-1;

        if(n==0)        //错误输入数据,特殊处理
            return matrix;

        //开始遍历
        while(tot <= n*n)
        {
            while(x==up && tot <= n*n)     //最上面一行
            {
                matrix[x][y] = tot;
                tot++;
                if(y == right)
                    x++;
                else
                    y++;
            }
            up += 1;

            while(y==right  && tot <= n*n)
            {
                matrix[x][y] = tot;
                tot++;
                if(x == down)
                    y--;
                else
                    x++;
            }
            right -= 1;

            while(x==down && tot <= n*n)
            {
                matrix[x][y] = tot;
                tot++;
                if(y == left)
                    x--;
                else
                    y--;
            }
            down -= 1;

            while(y==left && tot <= n*n)
            {
                matrix[x][y] = tot;
                tot++;
                if(x == up)
                    y++;
                else
                    x--;
            }
            left += 1;
        }

        //cout<<up<<down<<left<<right;
        return matrix;
    }
};

附上solution区代码:

class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            vector<vector<int> > ret( n, vector<int>(n) );
            int k = 1, i = 0;
            while( k <= n * n )
            {
                int j = i;
                    // four steps
                while( j < n - i )             // 1. horizonal, left to right
                    ret[i][j++] = k++;
                j = i + 1;
                while( j < n - i )             // 2. vertical, top to bottom
                    ret[j++][n-i-1] = k++;
                j = n - i - 2;
                while( j > i )                  // 3. horizonal, right to left 
                    ret[n-i-1][j--] = k++;
                j = n - i - 1;
                while( j > i )                  // 4. vertical, bottom to  top 
                    ret[j--][i] = k++;
                i++;      // next loop
            }
            return ret;
        }
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值