Scribbling

LeetCode: 48. Rotate Image 본문

Computer Science/Coding Test

LeetCode: 48. Rotate Image

focalpoint 2021. 9. 5. 20:18

Rotating a matrix 90 degrees clockwise can be achieved just by transposing + mirror reversing.

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m, n = len(matrix), len(matrix[0])
        for i in range(m):
            for j in range(i+1, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        
        for i in range(m):
            for j in range(n//2):
                matrix[i][j], matrix[i][n-j-1] = matrix[i][n-j-1], matrix[i][j]

'Computer Science > Coding Test' 카테고리의 다른 글

36. Valid Sudoku  (0) 2021.09.06
44. Wildcard Matching  (0) 2021.09.06
39. Combination Sum  (0) 2021.09.04
32. Longest Valid Parentheses  (0) 2021.09.04
LeetCode: 29. Divide Two Integers  (0) 2021.09.02