顺时针打印矩阵(Python and C++解法) 题目: 思路: C++解法: Python解法:

  输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
示例 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]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof

思路:

A00  A01  A02  A03

D10  E11  E12  B13

D20  G21  F22    B23

C30    C31  C32  B33 

第一圈的起点(0,0),第二圈的起点(1,1)...

打印循环终止条件:对于一个n*m矩阵,存在2*star < n and 2*start <m

每一行每一列的打印前提条件见C++注释。

C++解法:

 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int>>& matrix) {
 4         if (matrix.size() == 0)  return {};  // 返回空的方法
 5         int rows = matrix.size(), columns = matrix[0].size();
 6         vector<int> orderResult;  // 存储结果
 7         int start = 0;  // 起点
 8 
 9         while (rows > 2 * start && columns > 2 * start) {  // 打印终止的条件
10             int endX = columns - 1 - start;  // 终止行号
11             int endY = rows - 1 - start;  // 终止列号
12 
13             // 从左到右打印,这一步总会有 
14             for (int i = start; i <= endX; i++)
15                 orderResult.push_back(matrix[start][i]);
16             // 判断是否需要从上到下打印,终止行号需要大于起始行号
17             if (start < endY)
18                 for (int i = start + 1; i <= endY; i++)  // 从上到下打印
19                     orderResult.push_back(matrix[i][endX]);
20             // 判断是否需要从右到左打印,终止行/列号都需要大于起始行/列号,即至少两行两列
21             if (start < endX && start < endY)
22                 for (int i = endX - 1; i >= start; i--)
23                     orderResult.push_back(matrix[endY][i]);
24             // 判断是否需要从下到上打印,不仅终止列号需要大于起始列号,终止行号也要比起始行号大2,即至少三行两列
25             if (start < endX && start < endY - 1)
26                 for (int i = endY - 1; i >= start + 1; i--)
27                     orderResult.push_back(matrix[i][start]);
28 
29             start += 1;
30         }
31         return orderResult;
32     }
33 };

Python解法:

 1 class Solution:
 2     def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
 3         if len(matrix) == 0:
 4             return []
 5         rows = len(matrix)
 6         colums = len(matrix[0])
 7         start = 0
 8         orderResult = []
 9 
10         while rows > 2 * start and colums > 2 * start:
11             endX = colums - 1 - start
12             endY = rows - 1 - start
13 
14             for i in range(start, endX+1):  #  从左到右打印
15                 orderResult.append(matrix[start][i])
16 
17             if endY > start:  # 从上到下打印
18                 for i in range(start + 1, endY+1):
19                     orderResult.append(matrix[i][endX])
20 
21             if endX > start and endY > start:  # 从右到左打印
22                 for i in range(endX - 1, start - 1, -1):
23                     orderResult.append(matrix[endY][i])
24 
25             if endX > start and endY - 1 > start:  # 从下到上打印
26                 for i in range(endY - 1, start, -1):
27                     orderResult.append(matrix[i][start])
28 
29             start += 1
30         return orderResult