[C++]LeetCode: 110 Spiral Matrix (螺旋输出矩阵元素)

题目:

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 ]
]

You should return [1,2,3,6,9,8,7,4,5].

思路:我们来自己画一个螺旋线的行走轨迹。我们会发现,螺旋始终重复四个遍历过程,向右遍历(列递增)-->向下遍历(行递增)-->向左遍历(列递减)-->向上遍历(行递减)。但是在螺旋线行走的过程中,我们的行列的上下边界一直在变。所以我们维护四个变量,rowBegin,
rowEnd, colBegin, colEnd. 我们用这个四个变量来指示遍历时的边界。遍历终止条件是rowBegin > rowEnd || colBegin > colEnd. 需要注意的是,我们在向左遍历和向上遍历时,需要要遍历的行或者列是否存在,避免重复。

Attention:

1.
维护四个变量,来界定螺旋线遍历边界。

int rowBegin = 0;
int rowEnd = row - 1;
int colBegin = 0;
int colEnd = col - 1;

2.
螺旋行走条件必须满足。

while (rowBegin <= rowEnd && colBegin <= colEnd)

3.
向左遍历和向上遍历时,为防止重复,需要先判断行列坐标值。

if (rowBegin <= rowEnd)  //重要判断,防止重复
            {
                //向左列递减遍历
                for (int j = colEnd; j >= colBegin; j--)
                {
                    ret.push_back(matrix[rowEnd][j]);
                }

            }
            rowEnd--;   //遍历后,去掉此行

            if (colBegin <= colEnd)  //重要判断,防止重复
            {
                //向上行递减遍历
                for (int i = rowEnd; i >= rowBegin; i--)
                {
                    ret.push_back(matrix[i][colBegin]);
                }
            }
            colBegin++; //遍历后,去掉此列

复杂度:O(N),
N为矩阵的元素总数

AC Code:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        vector<int> ret;
        if (matrix.empty()) return ret;

        int row = matrix.size();
        int col = matrix[0].size();

        int rowBegin = 0;
        int rowEnd = row - 1;
        int colBegin = 0;
        int colEnd = col - 1;

        //螺旋曲线,运动轨迹总是一致的
        while (rowBegin <= rowEnd && colBegin <= colEnd)
        {
            //向右列递增遍历
            for (int j = colBegin; j <= colEnd; j++)
            {
                ret.push_back(matrix[rowBegin][j]);
            }
            rowBegin++; //遍历后,去掉此行

            //向下行递增遍历
            for (int i = rowBegin; i <= rowEnd; i++)
            {
                ret.push_back(matrix[i][colEnd]);
            }
            colEnd--;   //遍历后,去掉此列

            if (rowBegin <= rowEnd)  //重要判断,防止重复
            {
                //向左列递减遍历
                for (int j = colEnd; j >= colBegin; j--)
                {
                    ret.push_back(matrix[rowEnd][j]);
                }

            }
            rowEnd--;   //遍历后,去掉此行

            if (colBegin <= colEnd)  //重要判断,防止重复
            {
                //向上行递减遍历
                for (int i = rowEnd; i >= rowBegin; i--)
                {
                    ret.push_back(matrix[i][colBegin]);
                }
            }
            colBegin++; //遍历后,去掉此列
         }

         return ret;
    }
};

时间: 2024-11-07 23:45:32

[C++]LeetCode: 110 Spiral Matrix (螺旋输出矩阵元素)的相关文章

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

Leetcode 54:Spiral Matrix 螺旋矩阵

54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

[C++]LeetCode: 110 Spiral Matrix 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 ] ] 思路:这道题和Spiral Matrix的模型和解法一样.要求我们将1~n^2的数字

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

Spiral Matrix 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 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:螺旋数组,需

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

【LeetCode】Spiral Matrix 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 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

Java for LeetCode 059 Spiral Matrix 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 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

LeetCode[Array]: Spiral Matrix 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 ] ] 这个题目跟LeetCode[Array]: Spiral Matrix不同的是:这个题目并不

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw