当前位置:网站首页>54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
2022-07-05 04:47:00 【chenyfan_】
54. 螺旋矩阵 ●●
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]
- 模拟
四条边按顺序遍历,移动、判断边界。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(); // m 行
int n = matrix[0].size(); // n 列
int top = 0, right = n-1, left = 0, buttom = m-1; //边界索引值
int numsize = m*n;
int num = 0;
vector<int> ans(numsize);
while(true){
for(int i = left; i <= right; i++){
ans[num++] = matrix[top][i];
}
top++;
if(top>buttom) break; // 遍历 判断边界
for(int i = top; i <= buttom; i++){
ans[num++] = matrix[i][right];
}
right--;
if(left>right) break;
for(int i = right; i >= left; i--){
ans[num++] = matrix[buttom][i];
}
buttom--;
if(top>buttom) break;
for(int i = buttom; i >= top; i--){
ans[num++] = matrix[i][left];
}
left++;
if(left>right) break;
}
return ans;
}
};
59. 螺旋矩阵 II ●●
给你一个正整数 n ,生成一个包含 1 到 n 2 n^2 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix .
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
- 生成一个 n×n 空矩阵 ans,随后模拟整个向内环绕的填入过程:
- 定义当前左右上下边界 l,r,t,b,初始值 num = 1,迭代终止值 numsize = n * n;
- 当 num <= numsize 时,始终按照 从左到右 从上到下 从右到左 从下到上 填入顺序循环,每次填入后:
- 执行 num += 1:得到下一个需要填入的数字;
- 更新边界:例如从左到右填完后,上边界 t += 1,相当于上边界向内缩 1。
- 使用 num <= numsize 而不是 l < r || t < b 作为迭代条件,是为了解决当 n 为奇数时,矩阵中心数字无法在迭代过程中被填充的问题。
- 最终返回 ans 即可。
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n, 0));
int up = 0, down = n-1, left = 0, right = n-1;
int cnt = 1, num = n*n;
while(cnt <= num){
// 迭代条件,填充数 <= 格子数
for(int i = left; i <= right; ++i) ans[up][i] = cnt++; // 右移
++up;
for(int i = up; i <= down; ++i) ans[i][right] = cnt++; // 下移
--right;
for(int i = right; i >= left; --i) ans[down][i] = cnt++; // 左移
--down;
for(int i = down; i >= up; --i) ans[i][left] = cnt++; // 上移
++left;
}
return ans;
}
};
边栏推荐
- Introduction to RT thread kernel (5) -- memory management
- Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
- Cookie learning diary 1
- Mode in BST (binary tree & Notes on question brushing)
- 2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
- [groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
- 2022-2028 global and Chinese FPGA prototype system Market Research Report
- Function template
- [AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
- Flink cluster configuration
猜你喜欢
Uncover the seven quirky brain circuits necessary for technology leaders
可观测|时序数据降采样在Prometheus实践复盘
XSS injection
程序员应该怎么学数学
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
AutoCAD - continuous annotation
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
Detailed introduction of OSPF header message
随机推荐
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
[Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
A survey of automatic speech recognition (ASR) research
质量体系建设之路的分分合合
直播预告 | 容器服务 ACK 弹性预测最佳实践
Special information | finance, accounting, audit - 22.1.23
Introduction to RT thread kernel (4) -- clock management
What are the building energy-saving software
2022-2028 global and Chinese video coding and transcoding Market Research Report
Chapter 6 text processing tools for shell programming (awk)
Manually implement heap sorting -838 Heap sort
Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
The remainder operation is a hash function
49 pictures and 26 questions explain in detail what is WiFi?
flutter 对象和列表
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Wenet: E2E speech recognition tool for industrial implementation
Debug insights
Minor spanning tree
Data security -- 14 -- Analysis of privacy protection governance