48. Rotate Image (Medium)

https://leetcode.com/problems/rotate-image/

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

Example 2:

Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

Solutions

class Solution {

    // It's so easy to get wrong with the index of the matrix, especially based on offsets for
    // inner square.

    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length <= 1) {
            return;
        }

        // inter panel, i is the thickness
        // starting row index i, ending row index size - i - 1
        // starting column index i, ending column index size - i - 1

        for (int i = 0; i < matrix.length / 2; i++) {
            int start = i; // inclusive
            int end = matrix.length - i; // exclusive
            int len = end - start;

            // first backup the top row
            int[] tmp = new int[len];
            for (int j = 0; j < len; j++) {
                tmp[j] = matrix[start][start + j];
            }

            // write over top row with left column
            // copy from left column head to row tail, never use reverse order
            for (int j = 0; j < len; j++) {
                matrix[start][end - j - 1] = matrix[start + j][start];
            }

            // write over left column with bottom row
            for (int j = 0; j < len; j++) {
                matrix[start + j][start] = matrix[end-1][start+j];
            }

            // write over bottom row with right column
            for (int j = 0; j < len; j++) {
                matrix[end-1][start+j] = matrix[end-1-j][end-1];
            }

            // write right column with preserved top row
            for (int j = 0; j < len; j++) {
                matrix[start + j][end-1] = tmp[j];
            }
        }
    }
}

Incorrect Solutions

class Solution {

    // TODO Incorrect solution

    public void rotate(int[][] matrix) {
        int size = matrix.length;

        // inter panel, i is the thickness
        // starting row index i, ending row index size - i - 1
        // starting column index i, ending column index size - i - 1

        for (int i = 0; i < size / 2; i++) {
            int start = i;
            int end = size - 1 - i;

            // 1. swap top and right
            for (int j = 1; j < end; j++) {
                swap(matrix, start, start + j, start + j, end);
            }

            swap(matrix, start, start, start, end);
            swap(matrix, start, start, end, end);

            int left = start + 1;
            int right = end - 1;
            while (left < right) {
                swap(matrix, start, left, start, right);
                left++;
                right--;
            }

            // 2. swap top and bottom
            for (int j = 0; j < end; j++) {
                swap(matrix, start, start + j, end, start + j);
            }

            // 3. swap top and left
            for (int j = 1; j < end; j++) {
                swap(matrix, start, start + j, start + j, start);
            }

            left = start + 1;
            right = end - 1;
            while (left < right) {
                swap(matrix, start, left, start, right);
                left++;
                right--;
            }
        }
    }

    private void swap(int[][] matrix, int ax, int ay, int bx, int by) {
        int tmp = matrix[ax][ay];
        matrix[ax][ay] = matrix[bx][by];
        matrix[bx][by] = tmp;
    }
}

References

Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-12-03 11:01:18

results matching ""

    No results matching ""