• 048 Rotate Image 旋转图像


    给定一个 n × n 的二维矩阵表示一个图像。
    将图像旋转 90 度(顺时针)。
    注意:
    你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像。
    例 1:
    给出的输入矩阵 =
    [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ],
    旋转输入矩阵,使其变为 :
    [
      [7,4,1],
      [8,5,2],
      [9,6,3]
    ]
    例 2:
    给出的输入矩阵 =
    [
      [ 5, 1, 9,11],
      [ 2, 4, 8,10],
      [13, 3, 6, 7],
      [15,14,12,16]
    ],
    旋转输入矩阵,使其变为 :
    [
      [15,13, 2, 5],
      [14, 3, 4, 1],
      [12, 6, 8, 9],
      [16, 7,10,11]
    ]
    详见:https://leetcode.com/problems/rotate-image/description/

    Java实现:

    class Solution {
        public void rotate(int[][] matrix) {
            int n=matrix.length;
            //沿左上至右下对角线,交换对称对
            for(int i=0;i<n;++i){
                for(int j=i+1;j<n;++j){
                    int tmp=matrix[i][j];
                    matrix[i][j]=matrix[j][i];
                    matrix[j][i]=tmp;
                }
            }
            //水平翻转每一行
            for(int i=0;i<n;++i){
                for(int j=0;j<n/2;++j){
                    int tmp=matrix[i][j];
                    matrix[i][j]=matrix[i][n-1-j];
                    matrix[i][n-1-j]=tmp;
                }
            }
        }
    }
    

    参考:http://www.cnblogs.com/grandyang/p/4389572.html

    https://www.cnblogs.com/lightwindy/p/8564385.html

  • 相关阅读:
    【Coreforces 1253E】
    计数专题乱做
    PKUWC2020乱做
    多项式板子
    notepad
    2021.4.9
    2021.4.8
    2021.3.31
    2021.3.26
    2021.3.25
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8691779.html
Copyright © 2020-2023  润新知