当前位置:网站首页>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
边栏推荐
猜你喜欢
MySQL advanced part 1: index
4. 对象映射 - Mapping.Mapster
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
LaMDA 不可能觉醒吗?
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
Is it impossible for lamda to wake up?
Appium foundation - use the first demo of appium
2021apmcm post game Summary - edge detection
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
1.13 - RISC/CISC
随机推荐
数据可视化图表总结(二)
实时时钟 (RTC)
Basic explanation of typescript
Data visualization chart summary (II)
1039 Course List for Student
11-gorm-v2-03-basic query
6. Logistic model
927. Trisection simulation
MySQL advanced part 1: View
Is it impossible for lamda to wake up?
Liunx starts redis
什么是套接字?Socket基本介绍
一些工具的记录2022
Leetcode-6111: spiral matrix IV
Daily question 2006 Number of pairs whose absolute value of difference is k
leetcode-556:下一个更大元素 III
【LeetCode】Easy | 20. Valid parentheses
Chapter 6 relational database theory
TypeScript 基础讲解
C Primer Plus Chapter 15 (bit operation)