一天一道LeetCode系列

(一)题目

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

(二)解题

相当于顺时针打印矩阵。我想到的方法就是一圈一圈的打印,每一圈的起点分别是(0,0),(1,1)…..,那么退出循环打印的条件是什么呢?

我们可以分析3*3的矩阵有两圈,4*4的矩阵有两圈,5*5的矩阵有三圈,6*6的矩阵有三圈…..

假设m*n的矩阵,起点位置为(ori,ori),那么满足的条件为m>ori*2&&n>ori*2

具体思路见代码注释:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> ret;
        if(matrix.size() ==0) return ret;//矩阵为空的时候直接返回
        int ori=0;//每一圈的起点
        int row = matrix.size();
        int col = matrix[0].size();
        int px=ori,py=ori;
        int i , j , x, y;
        while(row>ori*2 && col > ori*2)//退出循环的条件
        {
                bool flag1 = false, flag2 = false, flag3 = false;//整圈打印需要连续,如果发生某一个方向没打印就下面的就不需要判断了
            for (i = px; i < col - px; i++)//从左往右
            {
                ret.push_back(matrix[py][i]);
                flag1 = true;
            }
            px = i-1;//i越界了,应该减1
            for (j = py+1; j < row - py && flag1; j++)//从上到下
            {
                ret.push_back(matrix[j][px]);
                flag2 = true;
            }
            py = j-1;//同上
            for (x = px-1; x >= ori&&flag2; x--)//从右往左
            {
                ret.push_back(matrix[py][x]);
                flag3 = true;
            }
            px = x+1;//同上
            for (y = py-1; y > ori &&flag3; y--)//从下到上
            {
                ret.push_back(matrix[y][px]);
            }
            px = ++ori;//更新起点x
            py = ori;//更新起点y
        }
        return ret;
    }
};

【一天一道LeetCode】#54. Spiral Matrix的更多相关文章

  1. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  2. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  5. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  6. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  7. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. [leetcode]54. Spiral Matrix螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  10. [leetcode]54. Spiral Matrix二维数组螺旋取数

    import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. Vasya the Hipster

    One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red so ...

  2. RAP在线接口管理统计部署

    文档: https://github.com/thx/RAP/wiki/home_cn centos上部署 参考:https://github.com/thx/RAP/wiki/deploy_on_c ...

  3. Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  4. main函数之后的调用

    main函数代表进程的主线程.程序开始执行时,系统为程序创建一个进程,main函数其实并不是首先被调用的函数,而是操作系统调用了C/C++运行期启动函数,该函数负责对C/C++ 运行期库进行初始化.它 ...

  5. 手动添加SSH支持、使用c3p0

    之前做的笔记,现在整理一下:大家有耐心的跟着做就能成功: SSH(struts2.spring.hibernate) *  struts2      *  充当mvc的角色 *  hibernate ...

  6. 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码

    原文出处: 平凡之路的博客   欢迎分享原创到伯乐头条 伯乐在线注:『阅读优秀代码是提高开发人员修为的一种捷径』http://t.cn/S4RGEz .之前@伯乐头条 曾发过一条微博:『C 语言进阶有 ...

  7. octave installation on RHEL6.4

    octave installation on RHEL6.4 rhel6.4上安装octave GNU Octave 是一种高级语言,主要设计用来进行数值计算,它是 MathWorks 出品的 Mat ...

  8. 1085. Perfect Sequence (25) -二分查找

    题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...

  9. OpenCV特征点检测匹配图像-----添加包围盒

    最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point ...

  10. Android初级教程进程间的通信AIDL

    在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...