在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
func spiralOrder(matrix [][]int) []int { if len(matrix) == 0 { return []int{} } dir := 1 row, col := 0, 0 top, right, bottom, left := 0, len(matrix[0])-1, len(matrix)-1, 0 var res []int for top <= bottom && left <= right { res = append(res, matrix[row][col]) switch dir { case 1: if col == right { dir = 2 top++ row++ continue } col++ case 2: if row == bottom { dir = 3 right-- col-- continue } row++ case 3: if col == left { dir = 4 bottom-- row-- continue } col-- case 4: if row == top { dir = 1 left++ col++ continue } row-- } } return res }
|
请发表评论