矩阵顺时针旋转90度

题目

矩阵顺时针旋转90度

解答

public class TransFerJuzhen {

    // 规律:新座标(i,j) = 老座标(n-j-1,i)
    public static int[][] transfer(int[][] origin, int n) {
        int[][] transferResult = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                transferResult[i][j]= origin[n-j-1][i];
            }
        }
        return transferResult;
    }

    public static void main(String[] args) {
        int n = 3;
        int[][] origin = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[][] transferResult = transfer(origin,n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(transferResult[i][j]+" ");
            }
            System.out.println();
        }
    }
}
点赞