• N x N 的矩阵,顺时针旋转


    第一种方法:

    先打印外圈,再打印内圈

    public class RotateMatrix1 {
    
        public static void rotate(int[][] matrix) {
            int tR = 0;
            int tC = 0;
            int bR = matrix.length - 1;
            int bC = matrix[0].length - 1;
            while (tR < bR) {
                rotateEdge(matrix, tR++, tC++, bR--, bC--);
            }
        }
    
        public static void rotateEdge(int[][] m, int a, int b, int c, int d) {
            int times = d - b; 
            int tmp = 0;
            for (int i = 0; i != times; i++) {
                tmp = m[a][b + i];
                m[a][b + i] = m[c - i][b];
                m[c - i][b] = m[c][d - i];
                m[c][d - i] = m[a + i][d];
                m[a + i][d] = tmp;
            }
        }
    
        public static void printMatrix(int[][] matrix) {
            for (int i = 0; i != matrix.length; i++) {
                for (int j = 0; j != matrix[0].length; j++) {
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            int[][] matrix = { { 1, 2, 3, 4 }, 
                               { 5, 6, 7, 8 },
                               { 9, 10, 11, 12 },
                               { 13, 14, 15, 16 } };
            printMatrix(matrix);
            rotate(matrix);
            System.out.println("********************");
            printMatrix(matrix);
    
        }
    }

    第二种方法:

    按照对角线交换后,再交换列

    public class RotateMatrix2 {
    
        //主对角线不变,主对角线对称的点互换位置
        public static void  symmetry(int[][] matrix) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < i; j++) {
                    int temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            }
        }
    
        //总体的效果是交换整列,假如总共有4列,那么第0列和第3列交换,第1列和第2列交换,
        //实现过程是可以一行一行交,第0行交换完了,再交换下一行
        public static void swapCol(int [][] matrix) {
            for(int i = 0; i<matrix.length; i++) {
                for(int j = 0; j<matrix.length/2; j++) {
                     int temp = matrix[i][j];
                     matrix[i][j] = matrix[i][matrix[0].length - 1 - j];
                     matrix[i][matrix[0].length - 1 - j] = temp;
                }
            }
        }
        
        public static void main(String[] args) {
            int[][] matrix = { { 1, 2, 3, 4 }, 
                               { 5, 6, 7, 8 },
                               { 9, 10, 11, 12 },
                               { 13, 14, 15, 16 } };
            printMatrix(matrix);
            symmetry(matrix);
            System.out.println("********************");
            printMatrix(matrix);
            swapCol(matrix);
            System.out.println("********************");
            printMatrix(matrix);
        }
        
        public static void printMatrix(int[][] matrix) {
            for (int i = 0; i != matrix.length; i++) {
                for (int j = 0; j != matrix[0].length; j++) {
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
  • 相关阅读:
    ksframework的xlua版本
    unity摄像机脚本
    代码重构:用工厂+策略模式优化过多的if else代码块
    在Unity中创建攻击Slot系统
    Unity运用GPU代替CPU处理和计算简单测试
    程序员工具集
    Unity开发-你必须知道的优化建议
    unity向量计算
    ClassFoo-IT知识花园
    BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
  • 原文地址:https://www.cnblogs.com/moris5013/p/11628910.html
Copyright © 2020-2023  润新知