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

分析

举个例子自己从头到尾把数字列出来,很容易就找到规律了:

假设一维数组的坐标为x,取值范围是xMin~xMax;二维数组的坐标为y,取值范围是yMin~yMax。(也就是数组表示为int[y][x])

1. 从左到右,y=yMin,x: xMin->xMax,yMin++

2. 从上到下,x=xMax,y: yMin->yMax,xMax--

3. 从右到左,y=yMax,x: xMax->xMin,yMax--

4. 从下到上,x=xMin,y: yMax->uMin,xMin++

结束条件,xMin==xMax或者yMin==yMax

 

还要要注意的地方:空数组的情况要处理。

 

C++实现代码:

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

class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix)
    {
        if(matrix.empty()||matrix[0].empty())
            return vector<int>();
        vector<int> ret;
        int xmin=0;
        int ymin=0;
        int xmax=matrix[0].size()-1;
        int ymax=matrix.size()-1;
        ret.push_back(matrix[0][0]);
        int i=0,j=0;
        while(1)
        {
            while(i<xmax)
                ret.push_back(matrix[j][++i]);
            if(++ymin>ymax)
                break;
            while(j<ymax)
                ret.push_back(matrix[++j][i]);
            if(--xmax<xmin)
                break;
            while(i>xmin)
                ret.push_back(matrix[j][--i]);
            if(--ymax<ymin)
                break;
            while(j>ymin)
                ret.push_back(matrix[--j][i]);
            if(++xmin>xmax)
                break;
        }
        return ret;
    }
};

int main()
{
    Solution s;
    vector<vector<int> > vec= {{1,2}};
    vector<int> result=s.spiralOrder(vec);
    for(auto a:result)
        cout<<a<<" ";
    cout<<endl;
}

 

时间: 2024-08-13 03:20:01

Spiral Matrix的相关文章

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

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> #inclu

[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]. 我想着把这个二维数组分成一层一层的环,然后一层

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

[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上所有的题目,并且贴上了博主的解法,随时随地都能

imageview-关于android ImageView Matrix变换矩阵的一个问题,求赐教!

问题描述 关于android ImageView Matrix变换矩阵的一个问题,求赐教! 关于android ImageView Matrix变换矩阵的一个问题,求赐教! 在获取ImageMatrix中的的缩放比率的时时候居然会得到0.负数等,这是种么回事? float[] values = new float[9]; mImageView.getImageMatrix().getValues(values); float scaleX = values[Matrix.MSCALE_X]; f

Android Paint、Canvas、Matrix使用讲解(一、Paint)

http://blog.csdn.net/tianjian4592/article/details/44336949 好了,前面主要讲了Animation,Animator 的使用,以及桌面火箭效果和水波纹效果,分别使用android框架和自己绘制实现,俗话说,工欲善其事,必先利其器,接下来几篇文章主要讲绘制中我们需要常使用的一些利器: Paint:画笔 Canvas:画布 Matrix:变换矩阵 绘制动效确实就像拿着笔在画布上面画画一样,而Paint就是我们拿着的笔,Canvas就是使用的画布