Skip to content

Commit 266903d

Browse files
feat: add Rotate Image solution
- Transpose + reverse algorithm implementation - Multiple approaches: Layer by Layer, Four-way Swap, Iterative - O(n²) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent 979e29d commit 266903d

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/**
2+
* Time Complexity: O(n²) - Two passes
3+
* Space Complexity: O(1) - In-place modification
4+
*/
5+
class Solution {
6+
public void rotate(int[][] matrix) {
7+
int n = matrix.length;
8+
9+
// Step 1: Transpose the matrix
10+
for (int i = 0; i < n; i++) {
11+
for (int j = i; j < n; j++) {
12+
int temp = matrix[i][j];
13+
matrix[i][j] = matrix[j][i];
14+
matrix[j][i] = temp;
15+
}
16+
}
17+
18+
// Step 2: Reverse each row
19+
for (int i = 0; i < n; i++) {
20+
int left = 0, right = n - 1;
21+
while (left < right) {
22+
int temp = matrix[i][left];
23+
matrix[i][left] = matrix[i][right];
24+
matrix[i][right] = temp;
25+
left++;
26+
right--;
27+
}
28+
}
29+
}
30+
}
31+
32+
// Alternative approach using layer by layer rotation
33+
class SolutionLayerByLayer {
34+
public void rotate(int[][] matrix) {
35+
int n = matrix.length;
36+
37+
for (int layer = 0; layer < n / 2; layer++) {
38+
int first = layer;
39+
int last = n - 1 - layer;
40+
41+
for (int i = first; i < last; i++) {
42+
int offset = i - first;
43+
44+
// Save top
45+
int top = matrix[first][i];
46+
47+
// Move left to top
48+
matrix[first][i] = matrix[last - offset][first];
49+
50+
// Move bottom to left
51+
matrix[last - offset][first] = matrix[last][last - offset];
52+
53+
// Move right to bottom
54+
matrix[last][last - offset] = matrix[i][last];
55+
56+
// Move top to right
57+
matrix[i][last] = top;
58+
}
59+
}
60+
}
61+
}
62+
63+
// Alternative approach using four-way swap
64+
class SolutionFourWaySwap {
65+
public void rotate(int[][] matrix) {
66+
int n = matrix.length;
67+
68+
for (int i = 0; i < n / 2; i++) {
69+
for (int j = i; j < n - 1 - i; j++) {
70+
int temp = matrix[i][j];
71+
matrix[i][j] = matrix[n - 1 - j][i];
72+
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
73+
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
74+
matrix[j][n - 1 - i] = temp;
75+
}
76+
}
77+
}
78+
}
79+
80+
// Alternative approach using iterative
81+
class SolutionIterative {
82+
public void rotate(int[][] matrix) {
83+
int n = matrix.length;
84+
85+
// Transpose
86+
for (int i = 0; i < n; i++) {
87+
for (int j = i; j < n; j++) {
88+
int temp = matrix[i][j];
89+
matrix[i][j] = matrix[j][i];
90+
matrix[j][i] = temp;
91+
}
92+
}
93+
94+
// Reverse rows
95+
for (int i = 0; i < n; i++) {
96+
for (int j = 0; j < n / 2; j++) {
97+
int temp = matrix[i][j];
98+
matrix[i][j] = matrix[i][n - 1 - j];
99+
matrix[i][n - 1 - j] = temp;
100+
}
101+
}
102+
}
103+
}
104+
105+
// Alternative approach using while loop
106+
class SolutionWhileLoop {
107+
public void rotate(int[][] matrix) {
108+
int n = matrix.length;
109+
110+
// Transpose
111+
for (int i = 0; i < n; i++) {
112+
for (int j = i; j < n; j++) {
113+
int temp = matrix[i][j];
114+
matrix[i][j] = matrix[j][i];
115+
matrix[j][i] = temp;
116+
}
117+
}
118+
119+
// Reverse rows
120+
for (int i = 0; i < n; i++) {
121+
int left = 0, right = n - 1;
122+
while (left < right) {
123+
int temp = matrix[i][left];
124+
matrix[i][left] = matrix[i][right];
125+
matrix[i][right] = temp;
126+
left++;
127+
right--;
128+
}
129+
}
130+
}
131+
}
132+
133+
// Alternative approach using enhanced for loop
134+
class SolutionEnhancedForLoop {
135+
public void rotate(int[][] matrix) {
136+
int n = matrix.length;
137+
138+
// Transpose
139+
for (int i = 0; i < n; i++) {
140+
for (int j = i; j < n; j++) {
141+
int temp = matrix[i][j];
142+
matrix[i][j] = matrix[j][i];
143+
matrix[j][i] = temp;
144+
}
145+
}
146+
147+
// Reverse rows
148+
for (int i = 0; i < n; i++) {
149+
int left = 0, right = n - 1;
150+
while (left < right) {
151+
int temp = matrix[i][left];
152+
matrix[i][left] = matrix[i][right];
153+
matrix[i][right] = temp;
154+
left++;
155+
right--;
156+
}
157+
}
158+
}
159+
}
160+
161+
// More concise version
162+
class SolutionConcise {
163+
public void rotate(int[][] matrix) {
164+
int n = matrix.length;
165+
166+
// Transpose
167+
for (int i = 0; i < n; i++) {
168+
for (int j = i; j < n; j++) {
169+
int temp = matrix[i][j];
170+
matrix[i][j] = matrix[j][i];
171+
matrix[j][i] = temp;
172+
}
173+
}
174+
175+
// Reverse rows
176+
for (int i = 0; i < n; i++) {
177+
int left = 0, right = n - 1;
178+
while (left < right) {
179+
int temp = matrix[i][left];
180+
matrix[i][left] = matrix[i][right];
181+
matrix[i][right] = temp;
182+
left++;
183+
right--;
184+
}
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)