https://leetcode.com/problems/rotate-image/description/
코드
from copy import deepcopy
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)
max_depth = n//2
for depth in range(max_depth+1):
length = n-depth*2-1
# 위쪽 저장
top = deepcopy(matrix[depth][depth+1:n-depth])
# 왼 -> 위
for i in range(length):
matrix[depth][n-1-depth-i] = matrix[depth+i][depth]
# 아래 -> 왼
for i in range(length):
matrix[depth+i][depth] = matrix[n-1-depth][depth+i]
# 오른 -> 아래
for i in range(length):
matrix[n-1-depth][depth+i] = matrix[n-1-depth-i][n-1-depth]
# 위 -> 오른
for i in range(length):
matrix[n-1-depth-i][n-1-depth] = top[length-1-i]
In-place로 이미지를 돌리래서 무슨 소리인가 했더니 2차원 매트릭스를 새로 생성하지 않고, 그 안에서 값들을 돌리라는 말이었다.
그래서 2차원 배열을 패스츄리처럼 생각하고 한겹씩 돌렸다.
'문제풀이 > 구현' 카테고리의 다른 글
[Python/파이썬] 백준 1041번 주사위 (0) | 2025.03.23 |
---|---|
[Python/파이썬] LeetCode 38번 Count and Say (0) | 2025.03.17 |
[Python/파이썬] 백준 8972번 미친 아두이노 (0) | 2025.03.07 |
[Python/파이썬] 소프티어 CPTI (0) | 2025.02.07 |
[Python/파이썬] 백준 15683번 감시 (0) | 2025.01.07 |