您的位置:首页 > 其它

LeetCode 48. Rotate Image(旋转)

2016-05-21 05:34 477 查看
原题网址:https://leetcode.com/problems/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) {
for(int i=0; i<matrix.length/2; i++) {
for(int j=0; j<(matrix.length+1)/2; j++) {
int t = matrix[i][j];
matrix[i][j] = matrix[matrix.length-1-j][i];
matrix[matrix.length-1-j][i] = matrix[matrix.length-1-i][matrix.length-1-j];
matrix[matrix.length-1-i][matrix.length-1-j] = matrix[j][matrix.length-1-i];
matrix[j][matrix.length-1-i] = t;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: