您的位置:首页 > 其它

Leetcode Rotate Image

2016-07-20 03:34 483 查看
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?

Difficulty: Medium

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

return;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: