zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

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

    Rotate the image by 90 degrees (clockwise).

    Follow up:
    Could you do this in-place?

    思路:

    1) 把matrix想象成四块,把第一块抠出来并copy一份,然后旋转式copy,代码较长。

    package rotation;
    
    public class RotateImage {
    
        public void rotate(int[][] matrix) {
            int n = matrix.length;
            int rows = n / 2;
            int cols = n % 2 == 0 ? rows : (rows + 1);
            int[][] tmp = new int[rows][cols];
            for (int i = 0; i < rows; ++i) {
                for (int j = 0; j < cols; ++j) {
                    tmp[i][j] = matrix[i][j];
                }
            }
            
            for (int i = rows; i < n; ++i) {
                for (int j = 0; j < cols; ++j) {
                    matrix[j][n - 1 - i] = matrix[i][j];
                }
            }
            
            for (int i = cols; i < n; ++i) {
                for (int j = rows; j < n; ++j) {
                    matrix[j][n - 1 - i] = matrix[i][j];
                }
            }
            
            for (int i = 0; i < cols; ++i) {
                for (int j = cols; j < n; ++j) {
                    matrix[j][n - 1 - i] = matrix[i][j];
                }
            }
            
            for (int i = 0; i < rows; ++i) {
                for (int j = 0; j < cols; ++j) {
                    matrix[j][n - 1 - i] = tmp[i][j];
                }
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // int[][] matrix = {{1,2,3},{5,6,7},{9,10,11}};
            int[][] matrix = {{1,2},{3,4}};
            RotateImage r = new RotateImage();
            r.rotate(matrix);
        }
    
    }

    2) 其实可以将上面的代码进行简化合并,如下

    package rotation;
    
    public class RotateImage {
    
        public void rotate(int[][] matrix) {
            int n = matrix.length;
            int rows = n / 2;
            int cols = n - rows;
            for (int i = 0; i < rows; ++i) {
                for (int j = 0; j < cols; ++j) {
                    int 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;
                }
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // int[][] matrix = {{1,2,3},{5,6,7},{9,10,11}};
            int[][] matrix = {{1,2},{3,4}};
            RotateImage r = new RotateImage();
            r.rotate(matrix);
        }
    
    }
  • 相关阅读:
    Python——数据类型之list、tuple
    Python——数据类型初步:Numbers
    Python——初识Python
    Python——开篇之词
    PAT——乙级1028
    PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名
    PAT——甲级1065:A+B and C(64bit) 乙级1010一元多项式求导
    PAT——甲级1046S:shortest Distance
    PAT——甲级1042:Shuffling Mashine
    特征值和特征向量
  • 原文地址:https://www.cnblogs.com/null00/p/5076029.html
Copyright © 2011-2022 走看看