当前位置:网站首页>Leetcode interview question 29 clockwise print matrix

Leetcode interview question 29 clockwise print matrix

2022-06-26 18:13:00 Maccy37

Code carrier is me !

Reference resources LeetCode The website links :https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/

subject : Enter a matrix , Print each number in clockwise order from the outside to the inside .

Example 1:

 Input :matrix = [[1,2,3],[4,5,6],[7,8,9]]
 Output :[1,2,3,6,9,8,7,4,5]

Example 2:

 Input :matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
 Output :[1,2,3,4,8,12,11,10,9,5,6,7]

Limit :

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

I saw 《 The finger of the sword offer》 The problem-solving inside still doesn't understand his code

How to solve the problem : Layer by layer simulation

You can think of a matrix as layers , First print the outermost element , Second, print the elements of the outer layer , Until the innermost element is printed .

Define the order of a matrix k The distance from the layer to the nearest boundary is kk All the vertices of . for example , The outermost elements of the lower matrix are all the 1  layer , The sub outer elements are the first 2  layer , The rest of the elements are the first 3 layer .

 [1, 1, 1, 1, 1, 1, 1],
 [1, 2, 2, 2, 2, 2, 1],
 [1, 2, 3, 3, 3, 2, 1],
 [1, 2, 2, 2, 2, 2, 1],
 [1, 1, 1, 1, 1, 1, 1]

  For each layer , Start at the top left and go through all the elements in a clockwise order . Suppose that the upper left corner of the current layer is located in (top,left), The lower right corner is located in (bottom,right), Traverse the elements of the current layer in the following order .

1. Traverse the upper element from left to right , In turn (top,left) To (top,right).

2. Traverse the right element from top to bottom , In turn (top+1,right) To (bottom,right)

3. If left<right And top<bottom, Then traverse the lower element from right to left , In turn (bottom,right-1) To (bottom,left+1), And traverse the left element from bottom to top , In turn (bottom,left) To (top+1,left)

fig1

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
       if(matrix.size()<=0||matrix[0].size()<=0)
          return {};

        int rows=matrix.size(),columns=matrix[0].size();
        int left=0,right=columns-1,top=0,bottom=rows-1;
        vector<int>order;

        while(left<=right&&top<=bottom)
        {
            for(int column=left;column<=right;column++)
            {
                order.push_back(matrix[top][column]);
            }

            for(int row=top+1;row<=bottom;row++)
            {
                order.push_back(matrix[row][right]);
            }
            if(left<right&&top<bottom)
            {
                for(int column=right-1;column>left;column--)
                {
                    order.push_back(matrix[bottom][column]);
                }
                for(int row=bottom;row>top;row--)
                {
                    order.push_back(matrix[row][left]);
                }
            }
             left++;
             right--;
             top++;
             bottom--;
        }
        return order;
    }
};

  It is easier to understand this way of thinking .

 

 

原网站

版权声明
本文为[Maccy37]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261807207192.html