题解 | #螺旋矩阵#
螺旋矩阵
http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
/**
*
* @param matrix int整型二维数组
* @return int整型一维数组
*/
func spiralOrder(matrix [][]int) []int {
res := []int{}
if len(matrix) == 0 {return res}
t := 0
b := len(matrix) - 1
l := 0
r := len(matrix[0]) - 1
for t <= b && l <= r {
//左->右
for i := l; i <= r; i++ {
res = append(res, matrix[t][i])
}
t++
//上->下
for i := t; i <= b; i++ {
res = append(res, matrix[i][r])
}
r--
//就一行一列,则直接停
if t > b || l > r {
break
}
//右->左
for i := r; i >= l; i-- {
res = append(res, matrix[b][i])
}
b--
//下->上
for i := b; i >= t; i-- {
res = append(res, matrix[i][l])
}
l++
}
return res
}