Sword-finger offer (C++)-JZ29: Print matrix clockwise (algorithm-simulation)

Author: Zhai Tianbao Steven
Copyright Statement: The copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

Topic description:

Enter a matrix and print out each number in clockwise order from outside to inside. For example, if you enter the following 4 X 4 matrix:

[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]

Then print out the numbers in sequence

[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]

data range:

0 <= matrix.length <= 100

0 <= matrix[i].length <= 100

Example:

enter:

[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

return value:

[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]

Problem-solving ideas:

This question examines algorithmic scenario simulation. Two ways to solve the problem.

1) Simulation boundary

       Think of the matrix as a multi-layer package. First traverse the outermost layer clockwise, then enter the inner layer and continue traversing until the upper, lower, left and right boundaries are interlaced.

2) Spiral matrix

       The annulus is a one-level traversal, and the boundary is a one-level traversal. Boundary traversal is divided into four sub-parts: upper, lower, left and right. Traverse with the combination of "1+4" to complete the spiral matrix.

Test code:

1) Simulation boundary

class Solution {
public:
    vector<int> printMatrix(vector<vector<int>> matrix) {
        vector<int> result;
        int size = int(matrix.size());
        // 处理矩阵为空的情况
        if(size == 0) 
            return result;
        // 左边界
        int left = 0; 
        // 右边界
        int right = matrix[0].size() - 1; 
        // 上边界
        int up = 0; 
        // 下边界
        int down = size - 1; 
        // 循环直到边界交错
        while(left <= right && up <= down){ 
            // 上边界从左到右
            for(int i = left; i <= right; i++) 
                result.push_back(matrix[up][i]); 
            // 上边界向下一格,并判断上下边界位置是否交错
            up++; 
            if(up > down)
                break;
            // 右边界从上到下
            for(int i = up; i <= down; i++) 
                result.push_back(matrix[i][right]);
            // 右边界向左一格,并判断左右边界位置是否交错
            right--; 
            if(left > right)
                break;
            // 下边界从右到左
            for(int i = right; i >= left; i--) 
                result.push_back(matrix[down][i]);
            // 下边界向上一格,并判断上下边界位置是否交错
            down--; 
            if(up > down)
                break; 
            // 左边界从下到上
            for(int i = down; i >= up; i--) 
                result.push_back(matrix[i][left]);
            // 左边界向右一格,并判断左右边界位置是否交错
            left++; 
            if(left > right)
                break;
        }
        return result;
    }
};

2) Spiral matrix

class Solution {
public:
    vector<int> printMatrix(vector<vector<int>> matrix) {       
        int row = int(matrix.size());
        int col = int(matrix[0].size());
        // 处理矩阵为空的情况
        if(row == 0 || col == 0) 
            return vector<int>(0);
        // 确认环数,环数与行列的最小值有关
        int minL = min(row, col);
        int ring = minL / 2 + minL % 2;
        // 螺旋矩阵遍历
        int count = row * col;
        int t = 0;
        vector<int> result(count);
        for(int k = 0; k < ring; ++k){
            // 当前环上边界遍历
            for(int p = k; p < col - k && t < count; ++p){
                result[t++] = matrix[k][p];
            }
            // 当前环右边界遍历
             for(int p = k + 1; p < row - k - 1 && t < count; ++p){
                result[t++] = matrix[p][col - k - 1];
            }
            // 当前环下边界遍历
             for(int p = col - k - 1; p >= k && t < count; --p){
                result[t++] = matrix[row - k - 1][p];
            }
            // 当前环左边界遍历
             for(int p = row - k - 2; p >= k + 1 && t < count; --p){
                result[t++] = matrix[p][k];
            }
        }
        return result;
    }
};

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/132553704