12
12
*/
13
13
public class _48 {
14
14
15
- //credit: https://discuss.leetcode.com/topic/9744/ac-java-in-place-solution-with-explanation-easy-to-understand
16
15
public void rotate_O1 (int [][] matrix ) {
17
16
/**First swap the elements on the diagonal, then reverse each row:
18
17
* 1, 2, 3 1, 4, 7 7, 4, 1
19
18
* 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2
20
19
* 7, 8, 9 3, 6, 9 9, 6, 3
21
- This could be done in O(1) space!
20
+ This is done in O(1) space!
22
21
**/
23
22
int m = matrix .length ;
24
23
int n = matrix [0 ].length ;
25
24
for (int i = 0 ; i < m ; i ++) {
26
- for (int j = i ; j < n ; j ++) {
25
+ for (int j = i ; j < n ; j ++) {/**ATTN: j starts from i, so that the diagonal changes with itself, no change.*/
27
26
int tmp = matrix [i ][j ];
28
27
matrix [i ][j ] = matrix [j ][i ];
29
28
matrix [j ][i ] = tmp ;
@@ -39,13 +38,12 @@ This could be done in O(1) space!
39
38
}
40
39
}
41
40
42
- //credit: https://discuss.leetcode.com/topic/6796/a-common-method-to-rotate-the-image
43
41
/**First swap the rows bottom up, then swap the element on the diagonal:
44
42
* 1, 2, 3 7, 8, 9 7, 4, 1
45
43
* 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2
46
44
* 7, 8, 9 1, 2, 3 9, 6, 3
47
45
* */
48
- //however, this is using O(n) of extra space
46
+ /**This is using O(n) of extra space*/
49
47
public void rotate_On (int [][] matrix ) {
50
48
int m = matrix .length ;
51
49
int n = matrix [0 ].length ;
0 commit comments