当前位置:网站首页>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;
}
};
边栏推荐
- [groovy] closure (closure as function parameter | code example)
- Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
- Séparation et combinaison de la construction du système qualité
- The principle of attention mechanism and its application in seq2seq (bahadanau attention)
- [crampon game] MC tutorial - first day of survival
- Detailed introduction of OSPF header message
- 【acwing】528. cheese
- 官宣!第三届云原生编程挑战赛正式启动!
- Data security -- 14 -- Analysis of privacy protection governance
- Fluent objects and lists
猜你喜欢

Thinking of 2022 American College Students' mathematical modeling competition

2021 higher education social cup mathematical modeling national tournament ABCD questions - problem solving ideas - Mathematical Modeling
![[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)](/img/aa/3c8b7b27e322417777d1315b9a5a8f.jpg)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)

【acwing】528. cheese

Web开发人员应该养成的10个编程习惯

Label exchange experiment

AutoCAD - Document Management
![[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens](/img/20/5c5550e6dabc76702f0e7ce3bef068.jpg)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens

直播預告 | 容器服務 ACK 彈性預測最佳實踐

直播预告 | 容器服务 ACK 弹性预测最佳实践
随机推荐
English topic assignment (26)
Practice | mobile end practice
775 Div.1 C. Tyler and strings combinatorial mathematics
揭秘技术 Leader 必备的七大清奇脑回路
Use assimp library to read MTL file data
猿人学第一题
线上故障突突突?如何紧急诊断、排查与恢复
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
#775 Div.1 C. Tyler and Strings 组合数学
PHP reads the INI file and writes the modified content
Special information | finance, accounting, audit - 22.1.23
3 minutes learn to create Google account and email detailed tutorial!
计组笔记(1)——校验码、原补码乘除计算、浮点数计算
Neural network and deep learning Chapter 1: introduction reading questions
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
2021 huashubei mathematical modeling idea + reference + paper
SQL set operation
程序员应该怎么学数学
2021 higher education social cup mathematical modeling national tournament ABCD questions - problem solving ideas - Mathematical Modeling

