Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 48 · Matrix

Rotate Image

LeetCode 48 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Transpose, then mirror each row.

நடுத்தரம்ArrayMathMatrix
LeetCode இல் திற ↗

நான் Rotate Image (LeetCode 48) ஐ இப்படித் தீர்த்தேன் — Matrix பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

A 90° clockwise rotation equals a transpose (swap across the main diagonal) followed by reversing each row. Both steps are in-place swaps.

Approach

  1. 1

    Swap matrix[i][j] with matrix[j][i] for j ≥ i.

  2. 2

    Reverse every row by swapping its ends inward.

நான் எப்படித் தீர்த்தேன்

Transpose + reflectPythonO(n²) time · O(1) space
class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        """
        #transpose
        [1][4][7]
        [2][5][8]
        [3][6][9]
        #reflect
        [7][4][1]
        [8][5][2]
        [9][6][3]
        """
        m = len(matrix)
        n = len(matrix[0])
        
        for i in range(m):
            for j in range(i,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]


ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Rotate Image — அடிக்கடி கேட்கப்படும் கேள்விகள்

Rotate Image பின்னாலுள்ள intuition என்ன?

A 90° clockwise rotation equals a transpose (swap across the main diagonal) followed by reversing each row. Both steps are in-place swaps.

Rotate Image (LeetCode 48) ஐ எப்படித் தீர்ப்பது?

1. Swap matrix[i][j] with matrix[j][i] for j ≥ i. 2. Reverse every row by swapping its ends inward.

Rotate Image இன் time மற்றும் space complexity என்ன?

Time: O(n²). Space: O(1). (Accepted approach: Transpose + reflect.)

Rotate Image இல் பொதுவான தவறுகள் என்ன?

Start the transpose inner loop at j = i, or every pair is swapped twice (back to the original).