矩阵旋转90渡的两种方法
矩阵旋转90度的两种方法
第二种:
显然,第二种的时间复杂度要比第一种小很多。
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; } } }
显然,第二种的时间复杂度要比第一种小很多。