LeetCode[Array]: 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].

一开始这个题目我想到的是用递归来做:首先扫描最外环并缩小矩阵,然后递归调用内部矩阵来完成整个矩阵的扫描。

扫描最外环并缩小矩阵的具体过程是:首先扫描第一行,扫描完去掉第一行;扫描最后一列,扫描的同时去掉最后一列;逆向扫描最后一行,扫描完去掉最后一行;逆向扫描第一列,扫描同时去掉第一列。这样扫描完成时矩阵也变小了,递归调用继续扫描变小的矩阵,直到所有元素都扫描完时即可。

根据以上思路,我的C++代码实现如下:

vector<int> spiralOrder(vector<vector<int> > &matrix) {
    vector<int> result;
    if (matrix.empty()) return result;

    // scan the first row
    result = matrix[0];
    matrix.erase(matrix.begin());
    if (matrix.empty()) return result;

    // scan the last col
    int colIdx = matrix[0].size() - 1;
    for (int i = 0; i < matrix.size(); ++i) {
        result.push_back(matrix[i][colIdx]);
        matrix[i].erase(matrix[i].begin() + colIdx);
    }
    if (matrix[0].empty()) return result;

    // scan the last row
    result.insert(result.end(), matrix.back().rbegin(), matrix.back().rend());
    matrix.erase(matrix.end() - 1);
    if (matrix.empty()) return result;

    // scan the first col
    for (int i = matrix.size() - 1; i >= 0; --i) {
        result.push_back(matrix[i][0]);
        matrix[i].erase(matrix[i].begin());
    }
    if (matrix[0].empty()) return result;

    // scan the inner matrix
    vector<int> inner = spiralOrder(matrix);
    result.insert(result.end(), inner.begin(), inner.end());
    return result;
}

后来在Discuss上逛了逛,发现这个题其实用迭代来解会更好,因为迭代不需要将矩阵缩小,直接用4个变量标志顶部扫描到哪一行、右边扫描到哪一列、底部扫描到哪一行、左边扫描到哪一列,然后每迭代一次完成一环的扫描。具体实现可以参考这里

时间: 2024-08-05 17:24:08

LeetCode[Array]: 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: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 - 螺旋输出矩阵中的元素

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】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 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input:

[LeetCode]59.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]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. 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][JavaScript]Spiral Matrix II

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 ] ] https://leetcode.com/problems/