题目:

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]. (Medium)

分析:

题目没有什么复杂的算法要应用,就是一行一列输出,处理好细节即可。

比较清晰的写法是搞一个rowBegin, rowEnd, colBegin, colEnd, 并在处理完一行/一列后更新值,并判断是否仍然满足 rowBegin <= rowEnd && colBegin <= colEnd

代码:

 class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
if (matrix.size() == ) {
return result;
}
int m = matrix.size(), n = matrix[].size();
int rowBegin = , rowEnd = m - , colBegin = , colEnd = n - ;
while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int i = colBegin; i <= colEnd; ++i ) {
result.push_back(matrix[rowBegin][i]);
}
rowBegin++;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowBegin; i <= rowEnd; ++i) {
result.push_back(matrix[i][colEnd]);
}
colEnd--;
if (colBegin > colEnd) {
break;
}
for (int i = colEnd; i >= colBegin; --i) {
result.push_back(matrix[rowEnd][i]);
}
rowEnd--;
if (rowBegin > rowEnd) {
break;
}
for (int i = rowEnd; i>= rowBegin; --i) {
result.push_back(matrix[i][colBegin]);
}
colBegin++;
}
return result;
}
};

LeetCode54 Spiral Matrix的更多相关文章

  1. 算法练习--LeetCode--54. Spiral Matrix 100%

      Spiral MatrixMedium Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  3. Leetcode54. Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

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

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

  5. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. [LeetCode] Spiral Matrix 螺旋矩阵

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

  7. LeetCode - 54. Spiral Matrix

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

  8. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  9. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. 了解真实的『REM』多终端屏幕适配

    作者:hbxeagle 链接:github.com/hbxeagle/rem/blob/master/README.md rem 作为一个低调的长度单位,由于手机端网页的兴起,在屏幕适配中得到重用.使 ...

  2. Clash Credenz 2014 Wild Card Round题解

    A题 简单模拟. /************************************************************************* > File Name: ...

  3. 关于python 环境变量

    1.默认命令行的启动的python 版本,这依赖于系统的环境变量. 见上一篇关于linux 环境变量的PATH 变量的设置 2.python 中 import 包的搜索路径, 即除了当前程序目录,能i ...

  4. HDU4578 Transformation (多操作线段树)

    传送门 终于过了这道题.. 要注意标记之间的影响,和add操作时更新求和的顺序. same 区间每个数设置为x标记 mult  区间每个数乘x标记 add  区间每个数加x标记 ①:当打same标记时 ...

  5. 实时查看linux网卡流量 的base脚本

    #!/bin/bash " ] do eth=$ RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{pri ...

  6. 系统日志和内核消息 $ dmesg$ less /var/log/messages$ less /var/log/secure$ less /var/log/auth

    查看错误和警告消息,比如看看是不是很多关于连接数过多导致? 看看是否有硬件错误或文件系统错误? 分析是否能将这些错误事件和前面发现的疑点进行时间上的比对.

  7. 【模板】tarjanLCA [2017年6月计划 学习tarjanLCA]

    P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...

  8. UE4物理模块(一)---概述与可视化调试

    UE4.21前的版本采用的是NVIDIA的PhysX做为其默认的物理引擎,用于计算3D世界的碰撞查询与物理模拟.自4.21版本开始改物理调用接口,但这并不是闲来重构代码,果然在2019GDC大会上放出 ...

  9. 优化 Tengine HTTPS 握手时间

    背景 网络延迟是网络上的主要性能瓶颈之一.在最坏的情况下,客户端打开一个链接需要DNS查询(1个 RTT),TCP握手(1个 RTT),TLS 握手(2个RTT),以及最后的 HTTP 请求和响应,可 ...

  10. GDOI模拟4.11~4.13总结

    总体情况 省选前的第一场模拟,就连续三天垫底滚粗了. 三天下来,只做了第一天的签到题,然后再做了一些水题的暴力,还不得分. 三天分数:100/400+40/400+90/400=230/1200,得了 ...