Skip to content

Commit 596556f

Browse files
committed
Rotate Image
1 parent 1f134cd commit 596556f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rotate-image/TonyKim9401.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TC: O(n^2)
2+
// visit all elements with nested for-loop
3+
// SC: O(1)
4+
// constant space complexity
5+
class Solution {
6+
public void rotate(int[][] matrix) {
7+
int n = matrix.length;
8+
9+
for (int i = 0; i < (n + 1) / 2; i++) {
10+
for (int j = 0; j < n / 2; j++) {
11+
int temp = matrix[n-1-j][i];
12+
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
13+
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
14+
matrix[j][n-1-i] = matrix[i][j];
15+
matrix[i][j] = temp;
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)