当前位置:网站首页>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
边栏推荐
- 可变电阻器概述——结构、工作和不同应用
- Overview of variable resistors - structure, operation and different applications
- 1.14 - assembly line
- Daily question 1342 Number of operations to change the number to 0
- TypeScript 基础讲解
- Simple selection sort of selection sort
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- [rust notes] 13 iterator (Part 2)
- 1040 Longest Symmetric String
- leetcode-6111:螺旋矩阵 IV
猜你喜欢
SQLMAP使用教程(一)
Groupbykey() and reducebykey() and combinebykey() in spark
MySQL advanced part 1: stored procedures and functions
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
MIT-6874-Deep Learning in the Life Sciences Week 7
Quickly use Amazon memorydb and build your own redis memory database
Data visualization chart summary (I)
MySQL advanced part 2: optimizing SQL steps
leetcode-6111:螺旋矩阵 IV
Doing SQL performance optimization is really eye-catching
随机推荐
Daily question 1342 Number of operations to change the number to 0
Leetcode backtracking method
Arduino 控制的 RGB LED 无限镜
实时时钟 (RTC)
927. 三等分 模拟
1041 Be Unique
Appium foundation - use the first demo of appium
QT判断界面当前点击的按钮和当前鼠标坐标
数据可视化图表总结(一)
[rust notes] 15 string and text (Part 1)
Collection: programming related websites and books
leetcode-1200:最小绝对差
leetcode-6108:解密消息
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
【Rust 笔记】15-字符串与文本(上)
Open source storage is so popular, why do we insist on self-development?
Usage scenarios of golang context
884. Uncommon words in two sentences
传统数据库逐渐“难适应”,云原生数据库脱颖而出
[rust notes] 17 concurrent (Part 1)