반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 48 | Rotate Image | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/rotate-image/
문제
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
조건
- n == matrix.length == matrix[i].length
- 1 <= n <= 20
- -1000 <= matrix[i][j] <= 1000
내 풀이
matrix가 있을 때, 중심으로부터 거리가 같은 숫자끼리 돌린다.
아래 그림에서 같은 색끼리 한 그룹이라고 생각하면 이해가 쉽다.
코드
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
N = len(matrix)
for m in range(int(N/2)):
for n in range(m, N-1-m):
temp = matrix[m][n]
matrix[m][n] = matrix[N-1-n][m]
matrix[N-1-n][m] = matrix[N-1-m][N-1-n]
matrix[N-1-m][N-1-n] = matrix[n][N-1-m]
matrix[n][N-1-m] = temp
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1448 | Count Good Nodes in Binary Tree | Python (0) | 2022.09.01 |
---|---|
[LeetCode] 417 | Pacific Atlantic Water Flow | Python (0) | 2022.08.31 |
[LeetCode] 200 | Number of Islands | Python (0) | 2022.08.29 |
[LeetCode] 383 | Ransom Note | Python (0) | 2022.08.25 |
[LeetCode] 326 | Power of Three | Python (0) | 2022.08.24 |
Comments