func spiralOrder(matrix [][]int) []int { var res []int row := len(matrix) if row == 0 { return res } column := len(matrix[0]) count := row * column c, i, j := 0, 0, 0 for count > 0 { if c % 4 == 0 { for t := 0; t < column; t++ { if i == 0 { res = append(res,matrix[i][j]) j ++ }else{ j ++ res = append(res,matrix[i][j]) } count-- } if i == 0 { j-- } c ++ row -- }else if c%4 == 1 { for t := 0; t < row; t++ { i++ res = append(res,matrix[i][j]) count-- } column-- c ++ }else if c%4 == 2 { for t := 0; t < column; t ++ { j-- res = append(res,matrix[i][j]) count-- } c ++ row -- }else { for t := 0; t < row; t ++ { i-- res = append(res,matrix[i][j]) count-- } c ++ column-- } } return res }