Skip to content

48. 旋转图像

48. 旋转图像

代码

javascript
// 48. 旋转图像:https://leetcode.cn/problems/rotate-image/description/
// 输入:[[1,2,3],[4,5,6],[7,8,9]]
// 输出:[[7,4,1],[8,5,2],[9,6,3]]

export function rotateImage (matrix) {
  const len = matrix.length
  for (let i = 0; i < Math.floor(len / 2); i++) {
    for (let j = 0; j < Math.floor((len + 1) / 2); j++) {
      const temp = matrix[i][j]
      matrix[i][j] = matrix[len - j - 1][i]
      matrix[len - j - 1][i] = matrix[len - i - 1][len - j - 1]
      matrix[len - i - 1][len - j - 1] = matrix[j][len - i - 1]
      matrix[j][len - i - 1] = temp
    }
  }
}
typescript
// 48. 旋转图像:https://leetcode.cn/problems/rotate-image/description/
// 输入:[[1,2,3],[4,5,6],[7,8,9]]
// 输出:[[7,4,1],[8,5,2],[9,6,3]]

export function rotateImage (matrix: number[][]): void {
  const len = matrix.length
  for (let i = 0; i < Math.floor(len / 2); i++) {
    for (let j = 0; j < Math.floor((len + 1) / 2); j++) {
      const temp = matrix[i][j]
      matrix[i][j] = matrix[len - j - 1][i]
      matrix[len - j - 1][i] = matrix[len - i - 1][len - j - 1]
      matrix[len - i - 1][len - j - 1] = matrix[j][len - i - 1]
      matrix[j][len - i - 1] = temp
    }
  }
}

测试代码

ts
import { expect, test } from 'vitest'
import { rotateImage } from './typescript.ts'
import { rotateImage as rotateImageJs } from './javascript.js'

test(`rotateImage`, () => {
  const matrix = [[1,2,3],[4,5,6],[7,8,9]]
  rotateImage(matrix)
  expect(matrix).toEqual([[7,4,1],[8,5,2],[9,6,3]])
})

test(`rotateImageJs`, () => {
  const matrix = [[1,2,3],[4,5,6],[7,8,9]]
  rotateImageJs(matrix)
  expect(matrix).toEqual([[7,4,1],[8,5,2],[9,6,3]])
})