1. 程式人生 > >leetCode 48.Rotate Image (旋轉影象) 解題思路和方法

leetCode 48.Rotate Image (旋轉影象) 解題思路和方法

Rotate Image

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?

思路:其實就是旋轉陣列,沒有什麼難度,程式碼如下:

public class Solution {
    public void rotate(int[][] matrix) {
    	int[][] a = new int[matrix.length][matrix.length];
    	//實現深拷貝
    	for(int i = 0; i < matrix.length; i++){
    		for(int j = 0; j < matrix.length;j++){
    			a[i][j] = matrix[i][j];
    		}
    	}
    	//資料旋轉
        for(int i = 0; i < a[0].length; i++){
            int k = 0;
            for(int j = a.length-1; j >=0; j--){
                matrix[i][k++] = a[j][i];
                //System.out.print(a[j][i] + " ");
            }
            //System.out.println("");
        }
    }
}