打印矩阵

剑指offer 顺时针打印矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
vector<int> printMatrix (vector<vector<int> > matrix ) {
int rows = matrix.size();
int cols = matrix[0].size();
vector<int> result;
if (rows == 0 ) {
return result;
}
int left = 0, right = cols - 1, top = 0, bottom = rows - 1;

while (top <= bottom && left <= right) {
//从左到右
for (int i = left; i <=right; i++) {
result.push_back(matrix[top][i]);
}
// 从上到下
for (int i = top + 1; i <= bottom; i++) {
result.push_back(matrix[i][right]);
}
//从右到左
if (top != bottom) {
for (int i = right - 1; i >= left; i--) {
result.push_back(matrix[bottom][i]);
}
}
//从下到上
if (left != right) {
for (int i = bottom - 1; i > top; i--) {
result.push_back(matrix[i][left]);
}
}
left++, right--, top++, bottom--; //更新控制量

}
return result;
}
};

从右向左和从下到上 要 分别考虑 单行 、 单列 两种特殊情况。 一圈结束后(→,↓,←,↑)记得更新控制量 i.e. 一圈的四个边界。

0%