mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
23 lines
485 B
Go
23 lines
485 B
Go
package leetcode
|
|
|
|
func spiralMatrixIII(R int, C int, r0 int, c0 int) [][]int {
|
|
res, round, spDir := [][]int{}, 0, [][]int{
|
|
{0, 1}, // 朝右
|
|
{1, 0}, // 朝下
|
|
{0, -1}, // 朝左
|
|
{-1, 0}, // 朝上
|
|
}
|
|
res = append(res, []int{r0, c0})
|
|
for i := 0; len(res) < R*C; i++ {
|
|
for j := 0; j < i/2+1; j++ {
|
|
r0 += spDir[round%4][0]
|
|
c0 += spDir[round%4][1]
|
|
if 0 <= r0 && r0 < R && 0 <= c0 && c0 < C {
|
|
res = append(res, []int{r0, c0})
|
|
}
|
|
}
|
|
round++
|
|
}
|
|
return res
|
|
}
|