Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e184b6

Browse files
committedMar 17, 2025
DaleStudy#281 Rotate Image
1 parent b7c4922 commit 3e184b6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

โ€Žrotate-image/forest000014.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
# Time Complexity: O(n^2)
3+
# Space Complexity: O(1)
4+
*/
5+
class Solution {
6+
public void rotate(int[][] matrix) {
7+
int n = matrix.length;
8+
9+
for (int i = 0; i < n / 2; i++) {
10+
for (int j = i; j < n - i - 1; j++) {
11+
// [i][j] -> [j][n - i - 1]
12+
// [j][n - i - 1] -> [n - i - 1][n - j - 1]
13+
// [n - i - 1][n - j - 1] -> [n - j - 1][i]
14+
// [n - j - 1][i] -> [i][j]
15+
// (๊ฐ ์ธ๋ฑ์Šค์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ฒดํฌํ•ด์„œ, ๊ฐ๊ฐ์˜ ๋“ฑ์žฅํšŸ์ˆ˜๊ฐ€ 4๋ฒˆ์ž„์„ ํ™•์ธํ•˜๋ฉด, ํ‹€๋ฆฐ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹˜์„ quickํ•˜๊ฒŒ ์ฒดํฌํ•ด๋ณผ ์ˆ˜๋Š” ์žˆ์Œ. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๋งž๋‹ค๋Š” ๋ณด์žฅ์€ ์•ˆ ๋จ.)
16+
17+
int tmp = matrix[i][j];
18+
matrix[i][j] = matrix[n - j - 1][i];
19+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
20+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
21+
matrix[j][n - i - 1] = tmp;
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.