• [LeetCode] Spiral Matrix II


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

    Solution:
    class Solution {
    public:
        int **used;
        vector<vector<int> > generateMatrix(int n) {
            vector< vector<int > > ans;
            if(n == 0) return ans;
            
            used = new int*[n];
            for(int i = 0;i < n;i++)
            {
                used[i] = new int[n];
                memset(used[i], 0, n * sizeof(int));
            }
            
            int x = 0, y = -1, count = 1, dir = 1; //1 for right, 2 for down, 3 for left, 4 for up
            bool flag = true;
            while(flag)
            {
                switch(dir)
                {
                    case 1:
                    //current direction is right, try continue
                    if(y + 1 < n && used[x][y + 1] == 0)
                        used[x][(y++) + 1] = count++;
                        else if(x + 1 < n && used[x + 1][y] == 0)
                            {
                                used[(x++) + 1][y] = count++;
                                dir = 2;
                            }
                            else
                                flag = false;   
                    break;
                    case 2:
                    //current direction is right, try continue
                    if(x + 1 < n && used[x + 1][y] == 0)
                        used[(x++) + 1][y] = count++;
                        else if(y - 1 >= 0 && used[x][y - 1] == 0)
                            {
                                used[x][(y--) - 1] = count++;
                                dir = 3;
                            }
                            else
                                flag = false;   
                    break;
                    case 3:
                    //current direction is right, try continue
                    if(y - 1 >= 0 && used[x][y - 1] == 0)
                        used[x][(y--) - 1] = count++;
                        else if(x - 1 >= 0 && used[x - 1][y] == 0)
                            {
                                used[(x--) - 1][y] = count++;
                                dir = 4;
                            }
                            else
                                flag = false;
                    break;
                    case 4:
                    //current direction is right, try continue
                    if(x - 1 >= 0 && used[x - 1][y] == 0)
                        used[(x--) - 1][y] = count++;
                        else if(y + 1 < n && used[x][y + 1] == 0)
                            {
                                used[x][(y++) + 1] = count++;
                                dir = 1;
                            }
                            else
                                flag = false;   
                    break;
                    default: break;
                }
            }
            
            for(int i = 0;i < n;i++)
            {
                vector<int > tmp;
                for(int j = 0;j < n;j++)
                    tmp.push_back(used[i][j]);
                ans.push_back(tmp);
            }
            return ans;
        }
    };
    
    
  • 相关阅读:
    C语言 · 最大最小值
    C语言 · 三个整数的排序
    C语言 · 简单加法
    C语言 · FJ的字符串
    C语言 · 分解质因数
    C语言 · 数的统计
    C语言 · 成绩的等级输出
    C语言 · 区间K大数查询
    shell学习目录
    数据库学习目录
  • 原文地址:https://www.cnblogs.com/changchengxiao/p/3590698.html
Copyright © 2020-2023  润新知