funcspiralOrder(_matrix: [[Int]]) -> [Int] { if matrix.isEmpty || matrix[0].isEmpty { return [] } var res = [Int]() var left =0 var top =0 var bottom = matrix.count -1 var right = matrix[0].count -1 while left <= right && top <= bottom { // 左上到右上 for col in left ... right { res.append(matrix[top][col]) } // 右上到右下 if top +1<= bottom { // for...in...写法需要判断top+1和bottom的大小 for row in top+1... bottom { res.append(matrix[row][right]) } } // left == right || top == bottom是最后一行或最后一列 if left < right && top < bottom { // 右下到左下 var col = right -1 while col >= left { res.append(matrix[bottom][col]) col -=1 } var row = bottom -1 while row > top { // 左下到左上 res.append(matrix[row][left]) row -=1 } } left +=1 right -=1 top +=1 bottom -=1 } return res }