19 螺旋矩阵

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

复制代码
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

复制代码
输入: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]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100
思路1

1、检查参数的合法性

2、从头开始循环,遇到边界就转弯

3、可以用额外的空间做标记,标记是否走过这个位置。

4、标记一个当前行和当前列,以及上下左右的边界

5、初始时当前行为0,当前列为0,上边界top是0,下边界bottom是m-1,左边界left是0,右边界right是n-1

6、开始循环往右循环,到右边界之后,转向下。当前行+1,当前列走到最后一列,上边界往下走一格。

7、从上往下循环,一直走到底部。当前行走到最后一行,左边界--,当前列--;

8、从右到左循环,循环到左边界。当前列走到左边界,底部边界--,当前行--;

9、从下往上循环,循环到上边界。当前

cpp 复制代码
vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> ans;
    int m=matrix.size();
    if(0==m) return ans;
    int n=matrix[0].size();
    if(0==n) return ans;

    vector<vector<int>> status(m, vector<int>(n,-1));

    int row=0;//当前行
    int col=0;//当前列
    int top=0,bottom=m-1,left=0,right=n-1;

    while(1){
        //cout<<"one: col"<<col<<"  right:"<<right<<endl;
        if(col>right) break;
        for(int i=col;i<=right;i++){  //从左往右
            ans.push_back(matrix[col][i]);
        }

        row++; //当前行+1
        col=right; //当前列到最后一列
        top++; //上边界+1
        //cout<<"two: row"<<row<<"  bottom:"<<bottom<<endl;
        if(row>bottom) break;
        for(int i=row;i<=bottom;i++){ //从上往下
            ans.push_back(matrix[i][col]);
        }

        row=bottom;
        right--;
        col--;
        //cout<<"three: col"<<col<<"  left:"<<left<<endl;
        if(col<left) break;
        for(int i=col;i>=left;i--){ //从右
            ans.push_back(matrix[row][i]);
        }

        col=left;
        bottom--;
        row--;
        //cout<<"three: row"<<row<<"  top:"<<top<<endl;
        if(row<top) break;
        for(int i=row;i>=top;i--){
            ans.push_back(matrix[i][col]);
        }

        row=top;
        col++;
        left++;
        // cout<<"当前行:"<<row<<" 当前列:"<<col<<" top:"<<top<<" bottom:"<<bottom<<" left:"<<left<<" right:"<<right<<endl;
        // cout<<"m*n="<<m*n<<"  ans.size():"<<ans.size()<<endl;
        if(ans.size()==m*n) break;
    }
    return ans;
}
思路21、按层旋转,从最外层开始。

2、每一层都有四个数字,上边界top,下边界bottom,左边界left,右边界right。

3、每剥一层,上边界+1,下边界-1,左边界+1,右边界-1。

4、剥的过程为,从左到右走一行,从上到下走一列,从右到左走一行,从下到上走一列。

5、决定有多少层的是(下边界-上边界)和(右边界-左边界)哪一个小

6、圈数=(边界差)/2+(边界差)%2。

7、for循环开始剥每一层,放入vector中。

cpp 复制代码
int MatrixToVector(vector<int>& ans,vector<vector<int>>& matrix,int top,int bottom,int left,int right){
    if(top>bottom||left>right||top<0||bottom<0||left<0||right<0) return -1;
    int m=matrix.size();
    //cout<<"top:"<<top<<"  bottom:"<<bottom<<"  left:"<<left<<" right:"<<right<<endl;
    if(m<=bottom||0==m) return -1;
    int n=matrix[0].size();
    if(n<=right||0==n) return -2;

    for(int i=left;i<=right;i++){
        ans.push_back(matrix[top][i]);
    }
    top++;

    if(top>bottom) return -3;
    for(int i=top;i<=bottom;i++){
        ans.push_back(matrix[i][right]);
    }
    right--;

    if(right<left) return -4;
    for(int i=right;i>=left;i--){
        ans.push_back(matrix[bottom][i]);
    }

    bottom--;
    if(bottom<top) return -5;
    for(int i=bottom;i>=top;i--){
		ans.push_back(matrix[i][left]);
    }
    left++;

    return 0;
}

vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> ans;
    int m=matrix.size();
    if(0==m) return ans;
    int n=matrix[0].size();
    if(0==n) return ans;

   	int top=0,bottom=m-1,left=0,right=n-1;
    int Minsize=(m)>(n)?(n):(m);
    int cycle=Minsize/2+Minsize%2;
    for(int i=0;i<cycle;i++){
        int ret=MatrixToVector(ans,matrix,top++,bottom--,left++,right--);
    }
    return ans;
}

推荐一个零声教育学习教程,个人觉得老师讲得不错,分享给大家:[Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,点击立即学习:链接

相关推荐
码完就睡2 小时前
数据结构——树、二叉树基础概念
数据结构·算法
VL——MOESR3 小时前
【LuoguP1967】货车运输【生成树】【倍增】
c++·算法·题解·倍增·生成树
手写码匠3 小时前
华为云Flexus+DeepSeek征文|Agent 记忆系统实战:用 DeepSeek-R1/V3 + Dify 会话变量打造跨会话长期记忆
人工智能·深度学习·算法·aigc
Olafur_zbj3 小时前
【AI】CUDA编程中的维度
人工智能·算法
坚持编程的菜鸟4 小时前
模拟实现memcpy
c语言·算法·模拟实现my_memcpy
wabs6664 小时前
关于图论【最短路径之Bellman_ford 算法|卡码网94.城市间货物运输的思考】
数据结构·算法·图论·卡码网·bellman_ford·求最短路径
MrZhao4004 小时前
On-Policy Distillation(OPD):为什么大模型后训练要在学生自己的轨迹上蒸馏?
算法
larito4 小时前
图形学基础(面试版)
线性代数·矩阵
朱峥嵘(朱髯)4 小时前
数据库如何根据全表 NDV 估算子集的 NDV
数据库·算法