[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;

    }

};

你可能感兴趣的:(LeetCode)