-
Notifications
You must be signed in to change notification settings - Fork 17
/
print_matrix_clock_wisely.php
81 lines (69 loc) · 1.67 KB
/
print_matrix_clock_wisely.php
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* 剑指 Offer 系列:顺时针打印矩阵
* @author 学院君
*/
/**
* 实现函数
* @param array $matrix
* @param int $columns
* @param int $rows
*/
function printMatrixClockWisely(array $matrix, int $columns, int $rows)
{
if (empty($matrix)) {
return;
}
$start = 0;
while ($columns > $start * 2 && $rows > $start * 2)
{
printMatrixInCircle($matrix, $columns, $rows, $start);
$start++;
}
}
/**
* 打印一圈
* @param array $matrix
* @param int $columns
* @param int $rows
* @param int $start
*/
function printMatrixInCircle(array $matrix, int $columns, int $rows, int $start)
{
$endX = $columns - 1 - $start;
$endY = $rows - 1 - $start;
// 从左到右打印第一行(顺时针)
for ($i = $start; $i <= $endX; $i++) {
$number = $matrix[$start][$i];
printf("%d ", $number);
}
// 从上到下打印一行
if ($start < $endY) {
for ($i = $start + 1; $i <= $endY; $i++) {
$number = $matrix[$i][$endX];
printf("%d ", $number);
}
}
// 从右到左打印一行
if ($start < $endX && $start < $endY) {
for ($i = $endY - 1; $i >= $start; $i--) {
$number = $matrix[$endY][$i];
printf("%d ", $number);
}
}
// 从下到上打印一行
if ($start < $endX && $start < $endY - 1) {
for ($i = $endY - 1; $i >= $start + 1; $i--) {
$number = $matrix[$i][$start];
printf("%d ", $number);
}
}
}
// 测试代码
$matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
];
printMatrixClockWisely($matrix, 4, 4);