当前位置:网站首页>LeetCode-54
LeetCode-54
2022-07-05 06:17:00 【GreedySnaker】
To give you one m That's ok n Columns of the matrix matrix , Please follow Clockwise spiral sequence , Returns all elements in the matrix .
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector <int> ans;
if (matrix.empty())
{
return ans; // If it is empty , Go straight back to
}
int up = 0; // Upper, lower, left and right boundaries
int down = matrix.size() - 1;
int left = 0;
int right = matrix[0].size() - 1;
while (true)
{
for (int i = left; i <= right; ++i)
{
// towards the right
ans.push_back(matrix[up][i]);
}
if (++up > down)/// Reset the upper boundary , If the upper boundary is greater than the lower boundary , Then the traversal is completed , The same below
{
break;
}
for (int i = up; i <= down; ++i)
{
// Down
ans.push_back(matrix[i][right]);
}
if (--right < left) // Reset the boundary
{
break;
}
for (int i = right; i >= left; --i)
{
// towards the left
ans.push_back(matrix[down][i]);
}
if (--down < up)// Reset the lower boundary
{
break;
}
for (int i = down; i >= up; --i)
{
// Up
ans.push_back(matrix[i][left]);
}
if (++left > right)// Reset the left boundary
{
break;
}
}
return ans;
}
};
Direct loop add array , At present, we can consider whether to add row arrays directly , Don't cycle
边栏推荐
- 【Rust 笔记】14-集合(下)
- Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
- Leetcode-3: Longest substring without repeated characters
- 多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
- 打印机脱机时一种容易被忽略的原因
- 【Rust 笔记】17-并发(上)
- 【Rust 笔记】17-并发(下)
- 4. Object mapping Mapster
- Overview of variable resistors - structure, operation and different applications
- Spark中groupByKey() 和 reduceByKey() 和combineByKey()
猜你喜欢
SPI 详解
实时时钟 (RTC)
2021apmcm post game Summary - edge detection
RGB LED infinite mirror controlled by Arduino
Leetcode stack related
Groupbykey() and reducebykey() and combinebykey() in spark
redis发布订阅命令行实现
Introduction to LVS [unfinished (semi-finished products)]
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
数据可视化图表总结(一)
随机推荐
LVS简介【暂未完成(半成品)】
RGB LED infinite mirror controlled by Arduino
SPI details
可变电阻器概述——结构、工作和不同应用
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
The sum of the unique elements of the daily question
leetcode-9:回文数
SQL三种连接:内连接、外连接、交叉连接
6. Logistic model
MySQL advanced part 1: stored procedures and functions
Leetcode-9: palindromes
Leetcode-6110: number of incremental paths in the grid graph
【LeetCode】Easy | 20. Valid parentheses
[rust notes] 16 input and output (Part 1)
Data visualization chart summary (I)
【Rust 笔记】14-集合(上)
Appium基础 — 使用Appium的第一个Demo
【Rust 笔记】17-并发(上)
[rust notes] 16 input and output (Part 2)
1041 Be Unique