Rotate Image

来源:互联网 发布:提词器软件 mac 编辑:程序博客网 时间:2024/05/16 15:15

题目原型:

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?

基本思路:

题目意思就是把图片(矩阵)顺时针旋转90度,分析得,先交换i,j即:A[i][j]与A[j][i]交换,然后逆置每行。

public void rotate(int[][] matrix){int i = 0;int j = 0;int k = 0;int temp = 0;for(i = 0;i<matrix.length;i++){for(j=i;j<matrix[0].length;j++){temp = matrix[i][j];matrix[i][j] = matrix[j][i];matrix[j][i] = temp;}}for(i=0;i<matrix.length;i++){for(j=0,k=matrix[0].length-1;j<k;j++,k--){temp = matrix[i][j];matrix[i][j] = matrix[i][k];matrix[i][k] = temp;}}}



0 0