Rotate Image

来源:互联网 发布:廊坊百度快照优化推广 编辑:程序博客网 时间:2024/05/16 04:06

Q:

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?


Solution:

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


0 0
原创粉丝点击