54. Spiral Matrix
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相关推荐
- 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 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 54. Spiral Matrix (Matrix)
设置toprow bottomrow leftcol rightcol来标记边界,然后对每一条边界进行循环 要是list的size等于matrix的size的话 就表明结束了 1 class Solu ...
- leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)
题目 https://leetcode.com/problems/spiral-matrix/ 题解 类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 .用 长宽 - pa ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- 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 ...
- LeetCode 54. Spiral Matrix--Python解法--螺旋排序
题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
最新文章
- 2022-2028年中国再生橡胶制造行业市场专项调研及投资前景规划报告
- iOS JSON解析 NSJSONReadingMutableContainers的作用:
- 解决apache配置问题小结
- 11-jQuery的事件绑定和解绑
- [转]自用类库整理之SqlHelper和MySqlHelper
- superhot预告片下载_预告片:裸指关节SOA
- LeetCode MySQL 1777. 每家商店的产品价格(行列转换)
- WCF学习之旅—第三个示例之四(三十)
- Java性能最后一个领域:去除垃圾回收器
- python脚本:检测字符串标识符
- 拓端tecdat|R语言中基于混合数据抽样(MIDAS)回归的HAR-RV模型预测GDP增长
- LAMP让开放源代码软件更安全 PHP是例外
- python 组态_西门子组态WinCC自学入门视频教程资源_48讲
- 思考模式之“5W1H“
- 计算机网络中表征数据传输有效性的指标是,表征数据传输有效性的指标是
- 求导四则运算以及三角函数求导 Derivative formulas
- 全球PM25实时可视化
- 华大单片机HC32L130J6TA入坑全纪录(一)
- 输入你的密码来连接到_手机无线网络可以连接但上不了网如何解决【详解】
- shell实现文本处理详解