Rotate Image

来源:互联网 发布:淘宝网改地址 编辑:程序博客网 时间:2024/06/09 20:46

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?


用个临时的数组,存下转换的位置再复制回去就行。

代码:

package codes;public class RotateImage {public static void main(String[] args) {int [][] matrix = {{1,2},{3,4}};new RotateImage().rotate(matrix);}public void rotate(int[][] matrix) {int size = matrix.length;int [][] res = new int[size][size];for(int i=0;i<size;i++){    for(int j = 0 ;j <size;j++){    res[j][size-1-i] = matrix[i][j];    }}for(int i=0;i<size;i++){    for(int j = 0 ;j <size;j++){     matrix[i][j] = res[i][j];    }}//for(int i=0;i<size;i++){//    for(int j = 0 ;j <size;j++){//    System.out.print(" "+matrix[i][j]);//    }//    System.out.println();//}}}



0 0