当前位置:编程学习 > JAVA >>

矩阵旋转90度的两种方法

java语言:
第一种:
[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;  
    }  
 
第二种:
[java]  
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;  
            }  
        }  
    }  
 
显然,第二种的时间复杂度要比第一种小很多。
 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,