엔지니어 게시판
LeetCode 솔루션 분류

[8/30] 48. Rotate Image

컨텐츠 정보

본문

Medium
11082548Add to ListShare

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.

 

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]

Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

 

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000
Accepted
1,015,099
Submissions
1,482,894

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 38 ms, faster than 91.36% of Python3 online submissions for Rotate Image.
Memory Usage: 13.8 MB, less than 74.32% of Python3 online submissions for Rotate Image.
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        top, bottom = 0, len(matrix)-1
        left, right = 0, len(matrix[0])-1
        
        while left < right or top < bottom:
            for i in range(right - left):
                # tl->tr
                temp_rt = matrix[top+i][right]
                matrix[top+i][right] = matrix[top][left+i]
                
                # rt->rb
                temp_rb = matrix[bottom][right-i]
                matrix[bottom][right-i] = temp_rt
                
                #rb->lb
                temp_lb = matrix[bottom-i][left]
                matrix[bottom-i][left] = temp_rb
                
                #lb->tl
                matrix[top][left+i] = temp_lb
            
            left +=1
            right -=1
            top+=1
            bottom-=1
        
        return matrix
    
전체 396 / 7 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 170 명
  • 오늘 방문자 4,384 명
  • 어제 방문자 5,475 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,093 명
알림 0