代码题(37)— 螺旋矩阵

1、54. 螺旋矩阵

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

示例 2:

输入:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

   思想:用左上和右下的坐标定位出一次要旋转打印的数据,一次旋转打印结束后,往对角分别前进和后退一个单位。

    提交代码时,主要的问题出在没有控制好后两个for循环,需要加入条件判断,防止出现单行或者单列的情况。

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& mat) {
        vector<int> res;
        if(mat.empty())
            return res;
        int m = mat.size();
        int n = mat[0].size();

        int left = 0, right = n-1, high = 0, low = m-1;
        while(left<=right && high<=low)
        {
            for(int i=left;i<=right;++i)
                res.push_back(mat[high][i]);
            for(int i=high+1;i<=low;++i)
                res.push_back(mat[i][right]);

            for(int i=right-1;i>=left && low>high;--i)//判断条件,避免出现单行情况(出现则不运行这里)
                res.push_back(mat[low][i]);

            for(int i=low-1;i>high && right>left;--i)//判断条件,避免出现单列情况(出现则不运行这里)
                res.push_back(mat[i][left]);
            left++,high++,right--,low--;
        }
        return res;
    }
};

 2、59. 螺旋矩阵 II

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n,vector<int>(n) );
        if(n<=0)
            return res;
        int left=0,right=n-1;
        int high=0,low=n-1;
        int num = 1;
        while(left<=right && high<=low)
        {
            for(int i=left;i<=right;++i)
                res[high][i] = num++;
            for(int i=high+1;i<=low;++i)
                res[i][right] = num++;
            for(int i=right-1;i>=left && high<low;--i)
                res[low][i] = num++;
            for(int i=low-1;i>high && left<right;--i)
                res[i][left] = num++;
            left++,high++,right--,low--;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/eilearn/p/9429602.html

时间: 2024-07-28 12:50:10

代码题(37)— 螺旋矩阵的相关文章

leecode第五十九题(螺旋矩阵 II)

class Solution { public: vector<vector<int>> generateMatrix(int n) { if(n==0)//特殊情况 { vector<vector<int>> empty; return empty; } vector<vector<int>> res; for(int i=0;i<n;i++)//要先初始化 { vector<int> zeros; for(int

leetCode 59.Spiral Matrix II (螺旋矩阵II) 解题思路和方法

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu

编程题之打印二维螺旋矩阵

当size=4时, 二维螺旋矩阵如下图所示: 规律总结 可以把这个二维矩阵看成一层套一层,如上图所示,1->4->7->10->12为第零层, 13->14->15->16为第一层,所以当size=4时,总共有两层.规律如下: 可分层数为:若size为偶数,层数=size/2, 若为奇数,则层数=(size+1)/2; 第n层一个方向上的数字的数量为:size-2*n; 这是因为第零层的一个方向上数字的数量就是size, 而第一层是size-2,...第n层就是s

NOIP2014-普及组复赛-第三题-螺旋矩阵

题目描述 Description 一个n行n列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第1行第1列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵中所有格子.根据经过顺序,在格子中依次填入1, 2, 3, ... , n,便构成了一个螺旋矩阵.2 下图是一个n = 4 时的螺旋矩阵. 1     2     3     4 12    13    14    5 11    16    15    6 10     9     8     7

1050. 螺旋矩阵(25) pat乙级题

本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为m行n列,满足条件:m*n等于N:m>=n:且m-n取所有可能值中的最小值. 输入格式: 输入在第1行中给出一个正整数N,第2行给出N个待填充的正整数.所有数字不超过104,相邻数字以空格分隔. 输出格式: 输出螺旋矩阵.每行n个数字,共m行.相邻数字以1个空格分隔,行末不得有多余空格. 输入样例: 12 37 76 20 98 76 42 53 95

从外向内扩的螺旋矩阵

形如: 1   2   3   4  5 16 17 18 19  6 15 24 25 20  7 14 23 22 21  8 13 12 11 10  9 如果我们需要打印这样从外部向内扩展的n * n矩阵. 分析: 可以把矩阵分为n/2个圈, (上面的例子分了两个圈,最外面的圈就是12345~16,另外一个圈就是17~24) 我们只需要以圈为单位,给二维数组中的每个数赋值.赋值的顺序也是按照数字的递增顺序(顺时针) Java代码如下: public class SpiralMatrix

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]

C版——打印螺旋矩阵

1.递归解法 递归解法如下: +--------------------------> X 轴 | 1   2   3   4 |  12 13 14 5 |  11 16 15 6 |  10 9   8   7 | Y轴 设元素1的坐标为(0,0),元素13的坐标为(1,1),--,任一元素的坐标为(x,y) 1 #include <stdio.h> 2 3 void SetMatrix(int **matrix, int x, int y, int start, int n) {

【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8,