当前位置:网站首页>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
边栏推荐
- Leetcode heap correlation
- QQ computer version cancels escape character input expression
- Leetcode stack related
- Leetcode array operation
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- Usage scenarios of golang context
- Leetcode-31: next spread
- 实时时钟 (RTC)
- Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
- 1.14 - 流水线
猜你喜欢

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively

数据可视化图表总结(二)

RGB LED infinite mirror controlled by Arduino

Appium foundation - use the first demo of appium

The connection and solution between the shortest Hamilton path and the traveling salesman problem

1.15 - input and output system

Redis publish subscribe command line implementation

4. 对象映射 - Mapping.Mapster
随机推荐
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
SQLMAP使用教程(一)
Sqlmap tutorial (1)
RGB LED infinite mirror controlled by Arduino
【Rust 笔记】15-字符串与文本(下)
Leetcode heap correlation
【Rust 笔记】14-集合(上)
WordPress switches the page, and the domain name changes back to the IP address
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
【Rust 笔记】15-字符串与文本(上)
Leetcode-6111: spiral matrix IV
884. Uncommon words in two sentences
liunx启动redis
[rust notes] 14 set (Part 1)
leetcode-1200:最小绝对差
927. 三等分 模拟
QT判断界面当前点击的按钮和当前鼠标坐标
Simple selection sort of selection sort
Golang uses context gracefully
[leetcode] day95 effective Sudoku & matrix zeroing