classSolution(object): defspiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ result = [] ifnot matrix: return result m, n = len(matrix), len(matrix[0]) top, bottom, left, right = 0, m - 1, 0, n - 1 while top <= bottom and left <= right: for j inrange(left, right + 1): result.append(matrix[top][j]) top += 1 for i inrange(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for j inrange(right, left - 1, -1): result.append(matrix[bottom][j]) bottom -= 1 if left <= right: for i inrange(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result
classSolution: defspiralOrder(self, matrix: List[List[int]]) -> List[int]: result = [] ifnot matrix: return result m, n = len(matrix), len(matrix[0]) top, bottom, left, right = 0, m - 1, 0, n - 1 while top <= bottom and left <= right: for j inrange(left, right + 1): result.append(matrix[top][j]) top += 1 for i inrange(top, bottom + 1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for j inrange(right, left - 1, -1): result.append(matrix[bottom][j]) bottom -= 1 if left <= right: for i inrange(bottom, top - 1, -1): result.append(matrix[i][left]) left += 1 return result
classSolution { funspiralOrder(matrix: Array<IntArray>): List<Int> { val result = mutableListOf<Int>() if (matrix.isEmpty() || matrix[0].isEmpty()) { return result } var top = 0 var bottom = matrix.size - 1 var left = 0 var right = matrix[0].size - 1 while (top <= bottom && left <= right) { for (i in left..right) { result.add(matrix[top][i]) } top++ for (i in top..bottom) { result.add(matrix[i][right]) } right-- if (top <= bottom) { for (i in right downTo left) { result.add(matrix[bottom][i]) } bottom-- } if (left <= right) { for (i in bottom downTo top) { result.add(matrix[i][left]) } left++ } } return result } }
classSolution{ List<int> spiralOrder(List<List<int>> matrix) { List<int> result = []; if (matrix.isEmpty || matrix[0].isEmpty) { return result; } int top = 0, bottom = matrix.length - 1; int left = 0, right = matrix[0].length - 1; while (top <= bottom && left <= right) { for (int i = left; i <= right; i++) { result.add(matrix[top][i]); } top++; for (int i = top; i <= bottom; i++) { result.add(matrix[i][right]); } right--; if (top <= bottom) { for (int i = right; i >= left; i--) { result.add(matrix[bottom][i]); } bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) { result.add(matrix[i][left]); } left++; } } return result; } }
funcspiralOrder(matrix [][]int) []int { result := []int{} iflen(matrix) == 0 || len(matrix[0]) == 0 { return result } top, bottom := 0, len(matrix)-1 left, right := 0, len(matrix[0])-1 for top <= bottom && left <= right { for i := left; i <= right; i++ { result = append(result, matrix[top][i]) } top++ for i := top; i <= bottom; i++ { result = append(result, matrix[i][right]) } right-- if top <= bottom { for i := right; i >= left; i-- { result = append(result, matrix[bottom][i]) } bottom-- } if left <= right { for i := bottom; i >= top; i-- { result = append(result, matrix[i][left]) } left++ } } return result }
# @param {Integer[][]} matrix # @return {Integer[]} defspiral_order(matrix) result = [] return result if matrix.empty? || matrix[0].empty? top, bottom = 0, matrix.length - 1 left, right = 0, matrix[0].length - 1 while top <= bottom && left <= right left.upto(right) do |i| result.push(matrix[top][i]) end top += 1 top.upto(bottom) do |i| result.push(matrix[i][right]) end right -= 1 if top <= bottom right.downto(left) do |i| result.push(matrix[bottom][i]) end bottom -= 1 end if left <= right bottom.downto(top) do |i| result.push(matrix[i][left]) end left += 1 end end result end
objectSolution{ defspiralOrder(matrix: Array[Array[Int]]): List[Int] = { var result: List[Int] = List() if (matrix.isEmpty || matrix(0).isEmpty) { return result } var top = 0 var bottom = matrix.length - 1 var left = 0 var right = matrix(0).length - 1 while (top <= bottom && left <= right) { for (i <- left to right) { result :+= matrix(top)(i) } top += 1 for (i <- top to bottom) { result :+= matrix(i)(right) } right -= 1 if (top <= bottom) { for (i <- right to left by -1) { result :+= matrix(bottom)(i) } bottom -= 1 } if (left <= right) { for (i <- bottom to top by -1) { result :+= matrix(i)(left) } left += 1 } } result } }