矩阵旋转90度的两种方法

2014-11-24 11:59:51 · 作者: · 浏览: 30
java语言:
第一种:
[java]
public static int[][] xuanzhuan(int a[][],int N){
int[][] b = new int[N][N];
for(int i=0;i
for(int j=0;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
{
int first = layer;
int last = N-1-layer;
int i;
for(i=layer; 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;
}
}
}
显然,第二种的时间复杂度要比第一种小很多。