AcWing 40. Golang顺时针打印矩阵, 模拟
原题链接
中等
作者:
痛苦鸭嘴笔
,
2020-01-31 01:38:44
,
所有人可见
,
阅读 769
Go 代码
func printMatrix(matrix [][]int) []int {
if len(matrix) == 0 {
return nil
}
r, c := len(matrix), len(matrix[0])
var res []int
for i, j := 0, 0; i <= (r-1)>>1 && j <= (c-1)>>1; i, j = i+1, j+1 {
res = append(res, matrix[i][j:c-j]...)
for k := i+1; k < r-i-1; k++ {
res = append(res, matrix[k][c-j-1])
}
if r-i-1 != i {
for k := c-j-1; k >= j; k-- {
res = append(res, matrix[r-i-1][k])
}
}
if c-j-1 != j {
for k := r-i-2; k >= i+1; k-- {
res = append(res, matrix[k][j])
}
}
}
return res
}