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 2 3, 6 9, 8 7, 4, 5.

题意为旋转输出矩阵元素.

人家想法: travel路线: right -> down -> left -> up 循环直到违反循环条件(rb <= re && cb <= ce)为止.

重点是控制 row_begin, row_end, col_begin and col_end. 注意循环条件,以及 left 和 up 时的条件判断.

下面代码展现的思想特清楚.

人个想法,自个代码:

\(O(n^2)\) time, \(O(1)\) extra space.

vector<int> spiralOrder(vector<vector<int>>& A) {
vector<int> res;
if (A.size() == 0) return res; const int m = A.size(), n = A[0].size();
int rb = 0, re = m - 1, cb = 0, ce = n - 1; while (rb <= re && cb <= ce) {
// right travel
for (int j = cb; j <= ce; j++)
res.push_back(A[rb][j]);
rb++; // down travel
for (int j = rb; j <= re; j++)
res.push_back(A[j][ce]);
ce--; // left travel
if (rb <= re) {
for (int j = ce; j >= cb; j--)
res.push_back(A[re][j]);
}
re--; // up travel
if (cb <= ce) {
for (int j = re; j >= rb; j--)
res.push_back(A[j][cb]);
}
cb++;
}
return res;
}

54. Spiral Matrix(中等)的更多相关文章

  1. LeetCode - 54. Spiral Matrix

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

  2. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  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 & 59. Spiral Matrix II

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

  5. Leetcode 54:Spiral Matrix 螺旋矩阵

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

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

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

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

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

  8. 54. Spiral Matrix

    题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...

  9. LeetCode OJ 54. Spiral Matrix

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

随机推荐

  1. Java练习(模拟扫雷游戏)

    要为扫雷游戏布置地雷,扫雷游戏的扫雷面板可以用二维int数组表示.如某位置为地雷,则该位置用数字-1表示, 如该位置不是地雷,则暂时用数字0表示. 编写程序完成在该二维数组中随机布雷的操作,程序读入3 ...

  2. sqlalchemy通过ssh连接远程mysql服务器

    首先需要一个模块sshtunnel,如果没有直接pip install sshtunnel from sshtunnel import SSHTunnelForwarder from sqlalche ...

  3. Centos:如何查找安装的jdk的目录

    使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径. 正确的方式是通过 which java: [tt@vddd ...

  4. 大数据处理架构hadoop

    Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台,为用户提供了系统底层细节透明的分布式基础架构.它是基于java语言开发的,具有很好的跨平台特性,其核心是分布式文件系 ...

  5. Java 异常基础详解

    目录 1. Java 中的异常 1.1 什么是异常? 1.2 什么是异常处理? 1.2.1 异常处理的优势 1.3 Java 异常类的层次结构 1.4 异常类型 1.5 检查和未检查异常之间的区别 1 ...

  6. [LeetCode] Array Nesting 数组嵌套

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...

  7. css 的一些知识点的整理

    css的一些标签整理   background-attachment: scroll;背景图可滚动 background-attachment: fixed; 固定背景的位置,不随着滚动条移动而移动 ...

  8. PHPCMS v9.6.0 任意文件上传漏洞分析

    引用源:http://paper.seebug.org/273/ 配置了php debug的环境,并且根据这篇文章把流程走了一遍,对phpstorm的debug熟练度+1(跟pycharm一样) 用户 ...

  9. Centos6.9连接工具设置

    由于vm下面的centos6.9这种操作环境非常的不友好,用起来非常的不方便, 所以我们需要用一个远程连接工具来连接,我们的虚拟机.我们使用的是teraterm. 下载地址:https://osdn. ...

  10. [LNOI 2014]LCA

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...