Skip to content

Commit fea8f1d

Browse files
refactor 48
1 parent 2ee3708 commit fea8f1d

File tree

1 file changed

+38
-6
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-6
lines changed

src/main/java/com/fishercoder/solutions/_48.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,44 @@
22

33
/**
44
* 48. Rotate Image
5-
*
6-
* You are given an n x n 2D matrix representing an image.
5+
6+
You are given an n x n 2D matrix representing an image.
77
88
Rotate the image by 90 degrees (clockwise).
99
10-
Follow up:
11-
Could you do this in-place?
10+
Note:
11+
You have to rotate the image in-place, which means you have to modify the
12+
input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
13+
14+
Example 1:
15+
Given input matrix =
16+
[
17+
[1,2,3],
18+
[4,5,6],
19+
[7,8,9]
20+
],
21+
rotate the input matrix in-place such that it becomes:
22+
[
23+
[7,4,1],
24+
[8,5,2],
25+
[9,6,3]
26+
]
27+
28+
Example 2:
29+
Given input matrix =
30+
[
31+
[ 5, 1, 9,11],
32+
[ 2, 4, 8,10],
33+
[13, 3, 6, 7],
34+
[15,14,12,16]
35+
],
36+
rotate the input matrix in-place such that it becomes:
37+
[
38+
[15,13, 2, 5],
39+
[14, 3, 4, 1],
40+
[12, 6, 8, 9],
41+
[16, 7,10,11]
42+
]
1243
*/
1344
public class _48 {
1445

@@ -22,7 +53,6 @@ public void rotate(int[][] matrix) {
2253
* 1, 2, 3 1, 4, 7 7, 4, 1
2354
* 4, 5, 6 becomes 2, 5, 8 becomes 8, 5, 2
2455
* 7, 8, 9 3, 6, 9 9, 6, 3
25-
This is done in O(1) space!
2656
**/
2757
int m = matrix.length;
2858
for (int i = 0; i < m; i++) {
@@ -51,11 +81,13 @@ This is done in O(1) space!
5181

5282
public static class Solution2 {
5383
/**First swap the rows bottom up, then swap the element on the diagonal:
84+
*
5485
* 1, 2, 3 7, 8, 9 7, 4, 1
5586
* 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2
5687
* 7, 8, 9 1, 2, 3 9, 6, 3
5788
*
58-
* This is using O(n) of extra space
89+
* Time: O(n^2)
90+
* Space: O(1)
5991
*/
6092
public void rotate(int[][] matrix) {
6193
int m = matrix.length;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy