矩阵旋转90°的两种方法-java

来源:互联网 发布:淘宝客如何设置返利 编辑:程序博客网 时间:2024/06/02 09:13

第1种:

public int[][] rotate(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-j-1][N-i-1] = a[i][N-1-j];}}return b;}

第2种:
public void rotate(int[][] 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;int 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;}}}




0 0
原创粉丝点击