You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Read full article from LeetCode – Rotate Image (Java)
Rotate the image by 90 degrees (clockwise).
public void rotate(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < Math.ceil(((double) n) / 2.); j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n-1-j][i]; matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; matrix[j][n-1-i] = temp; } } } |
Also refer to http://blog.csdn.net/kenden23/article/details/17200067
1 原矩阵计算出各个数值在新矩阵中的位置,然后一步到位转换成新的旋转矩阵
2 原矩阵到转置矩阵是很容易计算的,那么从转置矩阵到选择矩阵就只需要reverse每行的元素就可。Read full article from LeetCode – Rotate Image (Java)
No comments:
Post a Comment