Code28 print matrix clockwise

topic

Sword Finger Offer 29. Print matrix clockwise
Enter a matrix and print out 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]

限制:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100

Code

  • Recursive thinking
    • Get a clockwise number in the shape of "口" each time;
    • For each "口" glyph, you only need to know the starting point coordinates (upper left point) and the number of rows and columns;
      • Starting point coordinates, assuming (start_row, start_col)
      • Number of rows, assuming row_len
      • Number of columns, assuming col_len
    • The “口” shape relationship between the outer layer and the inner layer is that the coordinates of the starting point are both +1, and the number of rows and columns are both -2.
    • There are several different situations in the innermost layer:
      • "." glyph, such as 3*3 matrix
      • "一" font, such as 3*4 matrix
      • "|" glyph, such as 4*3 matrix
      • "口" glyph, such as 4*4 matrix
  • Code
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
    
    
 public:
  vector<int> spiralOrder(vector<vector<int>>& matrix) {
    
    
    int row = matrix.size();
    if (0 == row) {
    
    
      return vector<int>();
    }

    int col = matrix[0].size();
    if (0 == col) {
    
    
      return vector<int>();
    }

    return getCircle(matrix, 0, 0, row, col);
  }

 private:
  vector<int> getCircle(vector<vector<int>>& matrix,
                        int start_row,
                        int start_col,
                        int row_len,
                        int col_len) {
    
    
    if (row_len < 1 || col_len < 1) {
    
    
      return vector<int>();
    }

    vector<int> res;

    if (1 == row_len && 1 == col_len) {
    
    
      // .
      res.push_back(matrix[start_row][start_col]);
    } else if (1 == row_len) {
    
    
      // ——
      for (int i = 0; i < col_len; ++i) {
    
    
        res.push_back(matrix[start_row][start_col + i]);
      }
    } else if (1 == col_len) {
    
    
      // |
      for (int i = 0; i < row_len; ++i) {
    
    
        res.push_back(matrix[start_row + i][start_col]);
      }
    } else {
    
    
      // 口
      for (int i = 0; i < col_len; ++i) {
    
    
        res.push_back(matrix[start_row][start_col + i]);
      }

      for (int i = 1; i < row_len; ++i) {
    
    
        res.push_back(matrix[start_row + i][start_col + col_len - 1]);
      }

      for (int i = col_len - 2; i >= 0; --i) {
    
    
        res.push_back(matrix[start_row + row_len - 1][start_col + i]);
      }

      for (int i = row_len - 2; i > 0; --i) {
    
    
        res.push_back(matrix[start_row + i][start_col]);
      }
    }

    auto inner_circle = getCircle(matrix, start_row + 1, start_col + 1,
                                  row_len - 2, col_len - 2);

    copy(inner_circle.begin(), inner_circle.end(), back_inserter(res));

    return res;
  }
};

test

#include <iostream>
int main() {
    
    
   Solution s;
   {
    
    
     vector<vector<int>> matrix{
    
    {
    
    1, 2, 3, 4}, {
    
    5, 6, 7, 8}, {
    
    9, 10, 11, 12}};
     auto res = s.spiralOrder(matrix);
     for (auto i : res) {
    
    
       cout << i << " ";
     }
     cout << endl;
   }
   {
    
    
     vector<vector<int>> matrix{
    
    {
    
    3}, {
    
    2}};
     auto res = s.spiralOrder(matrix);
     for (auto i : res) {
    
    
       cout << i << " ";
     }
   }

  cin.get();
  return 0;
}
  • result
1 2 3 4 8 12 11 10 9 5 6 7
3 2

Guess you like

Origin blog.csdn.net/luoshabugui/article/details/110230310