Sword refers to Offer 29. Print the matrix clockwise (C++) gradually compress the boundary

Enter a matrix and print out each number in clockwise order from the outside to the inside.

Example 1:

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

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

limit:

0 <= matrix.length <= 100
0 <= matrix[i].length <= 100
Note: This question is the same as the main site 54 question: https://leetcode-cn.com/problems/spiral-matrix/

Problem-solving ideas:

根据题目示例 matrix = [[1,2,3],[4,5,6],[7,8,9]]
 的对应输出 [1,2,3,6,9,8,7,4,5] 可以发现,
顺时针打印矩阵的顺序是 “从左向右、从上向下、从右向左、从下向上” 循环。

Therefore, consider setting the "left, top, right, and bottom" four boundaries of the matrix to simulate the above matrix traversal sequence.

Insert picture description here

Algorithm flow:

1. Null value processing : When matrix is ​​empty, just return the empty list [] directly.
2. Initialization : The left, right, top and bottom four boundaries of the matrix l, r, t, b are used to print the result list res.
3. Cycling printing : "From left to right, from top to bottom, from right to left, from bottom to top" in four directions, do the following three things during printing in each direction (see the table below for specific information in each direction) ;
3-1, printing based on the boundary, i.e. the elements added in the order to the list res tail;
3-2, 11 contract inward boundary (representative has been printed);
3-3, it is determined whether the printing is completed (whether the boundary met), if It will pop out after printing.
4. Return value : Just return res.
Insert picture description here

class Solution 
{
    
    
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) 
    {
    
    
        if (matrix.empty()) return {
    
    };
        vector<int> res;
        int l = 0;                      //左边界
        int r = matrix[0].size() - 1;   //右边界
        int t = 0;                      //上边界
        int b = matrix.size() - 1;      //下边界
        while (true)
        {
    
    
            //left -> right 从左向右
            for (int i = l; i <= r; i++) res.push_back(matrix[t][i]);
            if (++t > b) break;//上边界越过下边界
            //top -> bottom
            for (int i = t; i <= b; i++) res.push_back(matrix[i][r]);
            if (--r < l) break;//右边界越过左边界
            //right -> left
            for (int i = r; i >= l; i--) res.push_back(matrix[b][i]);
            if (--b < t) break;//下边界越过上边界
            //bottom -> top
            for (int i = b; i >= t; i--) res.push_back(matrix[i][l]);
            if (++l > r) break;//左边界越过右边界
        }
        return res;
    }
};

Complexity analysis:

Time complexity O(MN): M and N are the number of rows and columns of the matrix, respectively.
Space complexity O(1): The four boundaries l, r, t, b use a constant size of extra space (res is the space that must be used).

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114677200