zoukankan      html  css  js  c++  java
  • 0048. Rotate Image (M)

    Rotate Image (M)

    题目

    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 NOTallocate 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]
    ]
    

    题意

    将给定的矩阵顺时针旋转90°,要求只能在原矩阵上进行修改。

    思路

    比较直接的方法是模拟旋转,如下图,经过旋转后,第一列变为第一行,第二列变为第二行……而第一行变为最后一列,第二行变为最后第二列……由此可以得到旋转规律:((i, j) ightarrow (j, n-i-1) ightarrow (n-i-1, n-j-1) ightarrow (n-j-1, i) ightarrow (i, j))

    为了不重复操作,只将下图中的区域①旋转3次得到区域②、③、④。

    另一种比较取巧的方法是,如下图,先沿着中心水平线上下翻转,再沿着左上到右下的主对角线翻转,就完成了顺时针旋转90°。


    代码实现

    Java

    旋转法

    class Solution {
        public void rotate(int[][] matrix) {
            int n = matrix.length;
            for (int i = 0; i < n / 2; i++) {
                for (int j = i; j < n - i - 1; j++) {
                    int temp = matrix[n - j - 1][i];
                    matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                    matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                    matrix[j][n - i - 1] = matrix[i][j];
                    matrix[i][j] = temp;
                }
            }
        }
    }
    

    翻转法

    class Solution {
        public void rotate(int[][] matrix) {
            int n = matrix.length;
            // 上下翻转
            for (int i = 0; i < n / 2; i++) {
                for (int j = 0; j < n; j++) {
                    swap(matrix, i, j, n - 1 - i, j);
                }
            }
            // 主对角线翻转
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < i; j++) {
                    swap(matrix, i, j, j, i);
                }
            }
        }
    
        private void swap(int[][] matrix, int i, int j, int x, int y) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[x][y];
            matrix[x][y] = temp;
        }
    }
    

    JavaScript

    旋转法

    /**
     * @param {number[][]} matrix
     * @return {void} Do not return anything, modify matrix in-place instead.
     */
    var rotate = function (matrix) {
      let n = matrix.length
      for (let i = 0; i < Math.trunc(n / 2); i++) {
        for (let j = i; j <= n - 1 - i; j++) {
          let tmp = matrix[i][j]
          matrix[i][j] = matrix[n - 1 - j][i]
          matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]
          matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]
          matrix[j][n - 1 - i] = tmp
        }
      }
    }
    

    翻转法

    /**
     * @param {number[][]} matrix
     * @return {void} Do not return anything, modify matrix in-place instead.
     */
    var rotate = function (matrix) {
      let n = matrix.length
    
      for (let i = 0; i < Math.trunc(n / 2); i++) {
        for (let j = 0; j < n; j++) {
          [matrix[i][j], matrix[n - 1 - i][j]] = [matrix[n - 1 - i][j], matrix[i][j]]
        }
      }
    
      for (let i = 0; i < n; i++) {
        for (let j = 0; j < i; j++) {
          [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]
        }
      }
    }
    
  • 相关阅读:
    20189222 《网络攻防技术》第一周作业
    apue.h 运行UNIX环境高编程序
    fflush()函数
    线性链表如何选择二级指针还是一级指针
    scanf()gets()fgets()区别
    淺談Coach思考模式
    Hello World
    C语言I博客作业04
    python模块:logging
    python模块:subprocess
  • 原文地址:https://www.cnblogs.com/mapoos/p/13217086.html
Copyright © 2011-2022 走看看