|
12 | 12 | */ |
13 | 13 | public class _48 { |
14 | 14 |
|
15 | | - public void rotate_O1(int[][] matrix) { |
16 | | - /**First swap the elements on the diagonal, then reverse each row: |
17 | | - * 1, 2, 3 1, 4, 7 7, 4, 1 |
18 | | - * 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2 |
19 | | - * 7, 8, 9 3, 6, 9 9, 6, 3 |
20 | | - This is done in O(1) space! |
21 | | - **/ |
22 | | - int m = matrix.length; |
23 | | - int n = matrix[0].length; |
24 | | - for (int i = 0; i < m; i++) { |
25 | | - for (int j = i; j < n; j++) { |
26 | | - /**ATTN: j starts from i, so that the diagonal changes with itself, no change.*/ |
27 | | - int tmp = matrix[i][j]; |
28 | | - matrix[i][j] = matrix[j][i]; |
29 | | - matrix[j][i] = tmp; |
| 15 | + /**Note: this is an n*n matrix, in other words, it's a square, this makes it easier as well.*/ |
| 16 | + |
| 17 | + public static class Solution1 { |
| 18 | + public void rotate(int[][] matrix) { |
| 19 | + /**First swap the elements on the diagonal, then reverse each row: |
| 20 | + * 1, 2, 3 1, 4, 7 7, 4, 1 |
| 21 | + * 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2 |
| 22 | + * 7, 8, 9 3, 6, 9 9, 6, 3 |
| 23 | + This is done in O(1) space! |
| 24 | + **/ |
| 25 | + int m = matrix.length; |
| 26 | + for (int i = 0; i < m; i++) { |
| 27 | + for (int j = i; j < m; j++) { |
| 28 | + /**ATTN: j starts from i, so that the diagonal changes with itself, results in no change.*/ |
| 29 | + int tmp = matrix[i][j]; |
| 30 | + matrix[i][j] = matrix[j][i]; |
| 31 | + matrix[j][i] = tmp; |
| 32 | + } |
30 | 33 | } |
31 | | - } |
32 | 34 |
|
33 | | - for (int i = 0; i < m; i++) { |
34 | | - for (int j = 0; j < n / 2; j++) { |
35 | | - int tmp = matrix[i][j]; |
36 | | - matrix[i][j] = matrix[i][n - 1 - j]; |
37 | | - matrix[i][n - 1 - j] = tmp; |
| 35 | + /**then reverse*/ |
| 36 | + for (int i = 0; i < m; i++) { |
| 37 | + int left = 0; |
| 38 | + int right = m - 1; |
| 39 | + while (left < right) { |
| 40 | + int tmp = matrix[i][left]; |
| 41 | + matrix[i][left] = matrix[i][right]; |
| 42 | + matrix[i][right] = tmp; |
| 43 | + left++; |
| 44 | + right--; |
| 45 | + } |
38 | 46 | } |
39 | 47 | } |
40 | 48 | } |
41 | 49 |
|
42 | | - /**First swap the rows bottom up, then swap the element on the diagonal: |
43 | | - * 1, 2, 3 7, 8, 9 7, 4, 1 |
44 | | - * 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2 |
45 | | - * 7, 8, 9 1, 2, 3 9, 6, 3 |
46 | | - * */ |
47 | | - /**This is using O(n) of extra space*/ |
48 | | - public void rotate_On(int[][] matrix) { |
49 | | - int m = matrix.length; |
50 | | - int n = matrix[0].length; |
51 | | - int top = 0; |
52 | | - int bottom = n - 1; |
53 | | - while (top < bottom) { |
54 | | - int[] tmp = matrix[top]; |
55 | | - matrix[top] = matrix[bottom]; |
56 | | - matrix[bottom] = tmp; |
57 | | - top++; |
58 | | - bottom--; |
59 | | - } |
| 50 | + public static class Solution2 { |
| 51 | + /**First swap the rows bottom up, then swap the element on the diagonal: |
| 52 | + * 1, 2, 3 7, 8, 9 7, 4, 1 |
| 53 | + * 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2 |
| 54 | + * 7, 8, 9 1, 2, 3 9, 6, 3 |
| 55 | + * |
| 56 | + * This is using O(n) of extra space |
| 57 | + */ |
| 58 | + public void rotate(int[][] matrix) { |
| 59 | + int m = matrix.length; |
| 60 | + int n = matrix[0].length; |
| 61 | + int top = 0; |
| 62 | + int bottom = n - 1; |
| 63 | + while (top < bottom) { |
| 64 | + int[] tmp = matrix[top]; |
| 65 | + matrix[top] = matrix[bottom]; |
| 66 | + matrix[bottom] = tmp; |
| 67 | + top++; |
| 68 | + bottom--; |
| 69 | + } |
60 | 70 |
|
61 | | - for (int i = 0; i < m; i++) { |
62 | | - for (int j = i + 1; j < n; j++) { |
63 | | - int tmp = matrix[i][j]; |
64 | | - matrix[i][j] = matrix[j][i]; |
65 | | - matrix[j][i] = tmp; |
| 71 | + for (int i = 0; i < m; i++) { |
| 72 | + for (int j = i + 1; j < n; j++) { |
| 73 | + int tmp = matrix[i][j]; |
| 74 | + matrix[i][j] = matrix[j][i]; |
| 75 | + matrix[j][i] = tmp; |
| 76 | + } |
66 | 77 | } |
67 | 78 | } |
68 | 79 | } |
|
0 commit comments