code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

048 Rotate Image

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * You are given an n x n 2D matrix that represents an image. Rotate the image by 90 degrees (clockwise).
 *
 * Example
 *
 * For
 *
 * a = [[1, 2, 3],
 *      [4, 5, 6],
 *      [7, 8, 9]]
 * the output should be
 *
 * rotateImage(a) =
 *     [[7, 4, 1],
 *      [8, 5, 2],
 *      [9, 6, 3]]
 */

function rotateImage(a) {
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < i; j++) {
      let temp = a[i][j]
      a[i][j] = a[j][i]
      a[j][i] = temp
    }    
  }
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < a[0].length/2; j++) {
      let temp = a[i][j]      
      a[i][j] = a[i][a[0].length-1-j]
      a[i][a[0].length-1-j] = temp     
    }    
  }
  return a
}

test('rotate Image', () => {
  expect(
    rotateImage([
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9],
    ]),
  ).toEqual([
    [7, 4, 1],
    [8, 5, 2],
    [9, 6, 3],
  ])
})

Created 2020-03-27T10:53:57+00:00 · Edit