48. Rotate Image

You are given annxn2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

思路:flip+switch

public class Solution {
    public void rotate(int[][] matrix) {
        int temp = 0;
        for (int i=0;i<matrix.length;i++){
            for (int j=i;j<matrix[0].length;j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

        for (int i=0;i<matrix.length;i++){
            for (int j=0;j<matrix[0].length/2;j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[i][matrix.length-1-j];
                matrix[i][matrix.length-1-j] = temp;
            }
        }
    }
}

results matching ""

    No results matching ""