https://youtu.be/fMSJSS7eO1w?si=XXhhhqR7m4_ogJ2l
https://youtu.be/I5WtA1Pax7Q?si=B9ySN5uPTH9Huk8R
from typing import List
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
주어진 n x n 2D 행렬을 90도 시계 방향으로 회전합니다.
이 작업은 제자리에서(in-place) 수행되어야 하며, 추가적인 2D 배열을 사용할 수 없습니다.
"""
l, r = 0, len(matrix) - 1 # 왼쪽과 오른쪽 경계를 설정
while l < r:
# 현재 회전에서 처리할 내부 사각형 레이어의 경계를 탐색
for i in range(r - l):
top, bottom = l, r # 위쪽과 아래쪽 경계 설정
"""
# i 를 고려 안했을때 기본 로직
# save the topleft
topleft = matrix[top][l]
# move bottom left into top left
matrix[top][l] = matrix[bottom][l]
# move bottom right into bottom left
matrix[bottom][l] = matrix[bottom][r]
# move top right into bottom right
matrix[bottom][r] = matrix[top][r]
# move top left into top righ
matrix[top][r] = topleft
"""
# 1. 현재 위치에서 왼쪽 위 값을 저장, save the topleft
topleft = matrix[top][l + i]
# 2. 왼쪽 아래 값을 왼쪽 위로 이동, move bottom left into top left
matrix[top][l + i] = matrix[bottom - i][l]
# 3. 오른쪽 아래 값을 왼쪽 아래로 이동, move bottom right into bottom left
matrix[bottom - i][l] = matrix[bottom][r - i]
# 4. 오른쪽 위 값을 오른쪽 아래로 이동, move top right into bottom right
matrix[bottom][r - i] = matrix[top + i][r]
# 5. 저장한 왼쪽 위 값을 오른쪽 위로 이동, move top left into top righ
matrix[top + i][r] = topleft
# 내부 레이어로 이동: 위쪽과 아래쪽 경계를 좁힘
l += 1
r -= 1
'LeetCode > Top Interview 150' 카테고리의 다른 글
205. Isomorphic Strings (1) | 2024.11.30 |
---|---|
383. Ransom Note (0) | 2024.11.30 |
54. Spiral Matrix (0) | 2024.11.30 |
36. Valid Sudoku (0) | 2024.11.30 |
76. Minimum Window Substring (0) | 2024.11.30 |