You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
[1,2,3], [7,4,1],
[4,5,6], -> [8,5,2],
[7,8,9] [9,6,3]
Example 2:
[ 5, 1, 9,11], [15,13, 2, 5],
[ 2, 4, 8,10], -> [14, 3, 4, 1],
[13, 3, 6, 7], [12, 6, 8, 9],
[15,14,12,16] [16, 7,10,11]
数学法。
1.坐标点变换推导规律+boolean[][] isTransformed。从第一个点到第二个点的变化画一画,根据到边界的长度可以推出:[x, y] -> [y, n - 1 - x]。用这个公式依次把新的xy代入可以得到: [x, y] -> [y, n - 1 - x] -> [n - 1 - x, n - 1 - y] -> [n - 1 - y, x]。另一个麻烦点是确定某个点曾经被前面的点链接换过没,可以用boolean[][]数组解决。
2.如果前者用额外数组不被面试官允许的话,需要指定要启动变化链的点所在区域:x: [0, (n + 1) / 2), y:[0, n / 2)。但其实现场推这个不靠谱像做过这题的,所以最好改口用第三个方法。
3.根据规律[x, y] -> [y, n - 1 - x],可以发现顺时针旋转==上下对称再对角线对称(xy互换)([x, y] -> [n - 1 - x, y] -> [y, n - 1 - x])。这个代码很好写了。其实逆时针旋转==左右对称再xy互换。写代码的时候主要ij范围难写,看着3*3, 4*4矩阵会好写一点。
法1实现:
class Solution { // [x, y] // [y, n - 1 - x] // [n - 1 - x, n - 1 - y] // [n - 1 - y, x] public void rotate(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0 || matrix.length != matrix[0].length) { return; } int n = matrix.length; boolean[][] isChanged = new boolean[n][n]; for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { if (isChanged[x][y]) { continue; } isChanged[x][y] = isChanged[n - 1 - y][x] = isChanged[n - 1 - x][n - 1 - y] = isChanged[y][n - 1 - x] = true; int temp = matrix[x][y]; matrix[x][y] = matrix[n - 1 - y][x]; matrix[n - 1 - y][x] = matrix[n - 1 - x][n - 1 - y]; matrix[n - 1 - x][n - 1 - y] = matrix[y][n - 1 - x]; matrix[y][n - 1 - x] = temp; } } } }
法3实现:
class Solution { public void rotate(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return; } // 上下翻转, 注意ij范围 int n = matrix.length; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } } // 对角线翻转,注意ij范围,看着3*3 4*4矩阵好写一点。 for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } } }