mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 13:00:22 +08:00
添加 0059.螺旋矩阵II PHP版本
This commit is contained in:
@ -426,6 +426,48 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
PHP:
|
||||||
|
```php
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* @param Integer $n
|
||||||
|
* @return Integer[][]
|
||||||
|
*/
|
||||||
|
function generateMatrix($n) {
|
||||||
|
// 初始化数组
|
||||||
|
$res = array_fill(0, $n, array_fill(0, $n, 0));
|
||||||
|
$mid = $loop = floor($n / 2);
|
||||||
|
$startX = $startY = 0;
|
||||||
|
$offset = 1;
|
||||||
|
$count = 1;
|
||||||
|
while ($loop > 0) {
|
||||||
|
$i = $startX;
|
||||||
|
$j = $startY;
|
||||||
|
for (; $j < $startY + $n - $offset; $j++) {
|
||||||
|
$res[$i][$j] = $count++;
|
||||||
|
}
|
||||||
|
for (; $i < $startX + $n - $offset; $i++) {
|
||||||
|
$res[$i][$j] = $count++;
|
||||||
|
}
|
||||||
|
for (; $j > $startY; $j--) {
|
||||||
|
$res[$i][$j] = $count++;
|
||||||
|
}
|
||||||
|
for (; $i > $startX; $i--) {
|
||||||
|
$res[$i][$j] = $count++;
|
||||||
|
}
|
||||||
|
$startX += 1;
|
||||||
|
$startY += 1;
|
||||||
|
$offset += 2;
|
||||||
|
$loop--;
|
||||||
|
}
|
||||||
|
if ($n % 2 == 1) {
|
||||||
|
$res[$mid][$mid] = $count;
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user