description:

螺旋输出一个矩阵.
Note:

Example:

Example 1:Input:
[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]Example 2:Input:
[[1, 2, 3, 4],[5, 6, 7, 8],[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

answer:

class Solution {
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {if (matrix.empty() || matrix[0].empty()) return {};int m = matrix.size(), n = matrix[0].size();vector<int> res;int up = 0, down = m - 1, left = 0, right = n - 1;while (true) {for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]);if (++up > down) break;for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]);if (-- right < left) break;for (int j = right; j >= left; --j) res.push_back(matrix[down][j]);if (--down < up) break;for (int i = down; i >= up; --i) res.push_back(matrix[i][left]);if (++left > right) break;}return res;}
};

relative point get√:

hint :

转载于:https://www.cnblogs.com/forPrometheus-jun/p/11262696.html

54. Spiral Matrix相关推荐

  1. LeetCode 54. Spiral Matrix

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

  2. LeetCode - 54. Spiral Matrix

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

  3. 54. Spiral Matrix (Matrix)

    设置toprow bottomrow leftcol rightcol来标记边界,然后对每一条边界进行循环 要是list的size等于matrix的size的话 就表明结束了 1 class Solu ...

  4. leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)

    题目 https://leetcode.com/problems/spiral-matrix/ 题解 类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 .用 长宽 - pa ...

  5. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  6. LeetCode:Spiral Matrix I II

    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--Python解法--螺旋排序

    题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...

  8. LeetCode Spiral Matrix II (生成螺旋矩阵)

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

  9. 59. Spiral Matrix II

    /** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...

最新文章

  1. 2022-2028年中国再生橡胶制造行业市场专项调研及投资前景规划报告
  2. iOS JSON解析 NSJSONReadingMutableContainers的作用:
  3. 解决apache配置问题小结
  4. 11-jQuery的事件绑定和解绑
  5. [转]自用类库整理之SqlHelper和MySqlHelper
  6. superhot预告片下载_预告片:裸指关节SOA
  7. LeetCode MySQL 1777. 每家商店的产品价格(行列转换)
  8. WCF学习之旅—第三个示例之四(三十)
  9. Java性能最后一个领域:去除垃圾回收器
  10. python脚本:检测字符串标识符
  11. 拓端tecdat|R语言中基于混合数据抽样(MIDAS)回归的HAR-RV模型预测GDP增长
  12. LAMP让开放源代码软件更安全 PHP是例外
  13. python 组态_西门子组态WinCC自学入门视频教程资源_48讲
  14. 思考模式之“5W1H“
  15. 计算机网络中表征数据传输有效性的指标是,表征数据传输有效性的指标是
  16. 求导四则运算以及三角函数求导 Derivative formulas
  17. 全球PM25实时可视化
  18. 华大单片机HC32L130J6TA入坑全纪录(一)
  19. 输入你的密码来连接到_手机无线网络可以连接但上不了网如何解决【详解】
  20. shell实现文本处理详解

热门文章

  1. Spring事件机制详解
  2. 并查集【CF731C】Socks
  3. SoapUI简介和入门实例解析
  4. 杭电oj2043密码
  5. 编译程序提示配置PKG_CONFIG_PATH
  6. NPOI操作Excel
  7. 如何让listview或者gridview设置到底刷新新数据
  8. Android PowerImageView实现,可以播放动画的强大ImageView
  9. Miner3D 数据分析软件
  10. 微信公众平台开发(44)历史上的今天