矩阵旋转90度的两种方法

来源:互联网 发布:剑灵南素柔捏脸数据 编辑:程序博客网 时间:2024/06/09 16:49

java语言:

第一种:

public static int[][] xuanzhuan(int a[][],int N){int[][] b = new int[N][N];for(int i=0;i<N;i++){for(int j=0;j<N;j++){b[N-1-j][N-1-i] = a[i][N-1-j];}}return b;}

第二种:

public static void rotate(char a[][],int N){    int layer;    for(layer=0; layer<N/2; layer++)    {        int first = layer;                      int last = N-1-layer;                       int i;        for(i=layer; i<last; i++)        {                int offset = i-layer;                char top = a[first][i];                a[first][i] = a[last-offset][first];                a[last-offset][first] = a[last][last-offset];                a[last][last-offset] = a[i][last];                a[i][last] = top;        }    }}

显然,第二种的时间复杂度要比第一种小很多。

原创粉丝点击