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

C++实现代码:(注意奇数和偶数的不同)

#include<iostream>
#include<vector>
using namespace std;

class Solution
{
public:
    vector<vector<int> > generateMatrix(int n)
    {
        if(n==0)
            return vector<vector<int> >();
        vector<vector<int> > vec(n,vector<int>(n));
        int i,j;
        int count=1;
        i=0,j=0;
        while(((n%2)&&(count<n*n))||((n%2==0)&&(count<=n*n)))
        {
            for(; j<n-i-1; j++)
            {
                vec[i][j]=count;
                count++;
            }
            for(; i<j; i++)
            {
                vec[i][j]=count;
                count++;
            }
            for(; j>n-i-1; j--)
            {
                vec[i][j]=count;
                count++;
            }
            for(; i>j; i--)
            {
                vec[i][j]=count;
                count++;
            }
            i++;
            j++;
        }
        if(n%2)
            vec[i][j]=count;
        return vec;
    }
};

int main()
{
    Solution s;
    vector<vector<int> > result=s.generateMatrix(4);
    for(auto a:result)
    {
        for(auto v:a)
            cout<<v<<" ";
        cout<<endl;
    }
    cout<<endl;
}

 改进的方法,与Spiral Matrix类似的方法:

class Solution {
public:
    vector<vector<int> > generateMatrix(int n) {
        if(n<=0)
            return vector<vector<int> >();
        int count=1;
        int xmin=0;
        int xmax=n-1;
        int ymin=0;
        int ymax=n-1;
        vector<vector<int> > res(n,vector<int>(n));
        int i=0;
        int j=0;
        res[i][j]=count++;
        while(count<=n*n)
        {
            while(j<xmax) res[i][++j]=count++;
            if(++ymin>ymax)
                break;
            while(i<ymax) res[++i][j]=count++;
            if(--xmax<xmin)
                break;
            while(j>xmin) res[i][--j]=count++;
            if(--ymax<ymin)
                break;
            while(i>ymin) res[--i][j]=count++;
            if(++xmin>xmax)
                break;
        }
        return res;
    }
};

 

 

时间: 2024-10-22 13:13:08

Spiral Matrix II的相关文章

[LeetCode]59.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 ] ] [分析] 模拟 [代码] /**-------------------------

[LeetCode]240.Search a 2D Matrix II

题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to botto

[LeetCode]--54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 我想着把这个二维数组分成一层一层的环,然后一层

Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 分析 举个例子自己从头到尾把数字列出来,很容易就

[LeetCode] Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

[经典面试题]蛇形矩阵(螺旋矩阵)

[1.打印蛇形矩阵] Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. [代码] /**----

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能