`
SunnyYoona
  • 浏览: 366653 次
社区版块
存档分类
最新评论

[LeetCode]59.Spiral Matrix II

 
阅读更多

【题目】

Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.

For example,
Givenn=3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

【分析】

模拟

【代码】

    /**------------------------------------
    *   日期:2015-02-04
    *   作者:SJF0115
    *   题目: 59.Spiral Matrix II
    *   网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            vector<vector<int> > matrix(n,vector<int>(n,0));
            if(n <= 0){
                return matrix;
            }//if
            int count = n * n;
            int index = 1;
            int x = 0,y = -1;
            while(index <= count){
                // right
                ++y;
                while(y < n && matrix[x][y] == 0){
                    matrix[x][y++] = index;
                    ++index;
                }//while
                --y;
                // down
                ++x;
                while(x < n && matrix[x][y] == 0){
                    matrix[x++][y] = index;
                    ++index;
                }//while
                --x;
                // left
                --y;
                while(y >= 0 && matrix[x][y] == 0){
                    matrix[x][y--] = index;
                    ++index;
                }//while
                ++y;
                // up
                --x;
                while(x >= 0 && matrix[x][y] == 0){
                    matrix[x--][y] = index;
                    ++index;
                }//while
                ++x;
            }//while
            return matrix;
        }
    };

    int main(){
        Solution s;
        int n = 5;
        vector<vector<int> > matrix = s.generateMatrix(n);
        // 输出
        for(int i = 0;i < n;++i){
            for(int j = 0;j < n;++j){
                cout<<matrix[i][j]<<"  ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

【代码二】

    /**------------------------------------
    *   日期:2015-02-05
    *   作者:SJF0115
    *   题目: 59.Spiral Matrix II
    *   网址:https://oj.leetcode.com/problems/spiral-matrix-ii/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
   class Solution {
    public:
        vector<vector<int> > generateMatrix(int n) {
            vector<vector<int> > matrix(n,vector<int>(n,0));
            if(n <= 0){
                return matrix;
            }//if
            int count = n * n;
            int index = 1;
            int beginX = 0,endX = n - 1;
            int beginY = 0,endY = n - 1;
            while(index <= count){
                // right
                for(int i = beginY;i <= endY;++i){
                    matrix[beginX][i] = index;
                    ++index;
                }//for
                ++beginX;
                // down
                for(int i = beginX;i <= endX;++i){
                    matrix[i][endY] = index;
                    ++index;
                }//for
                --endY;
                // left
                for(int i = endY;i >= beginY;--i){
                    matrix[endX][i] = index;
                    ++index;
                }//for
                --endX;
                // up
                for(int i = endX;i >= beginX;--i){
                    matrix[i][beginY] = index;
                    ++index;
                }
                ++beginY;
            }//while
            return matrix;
        }
    };


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics