Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 48 · Matrix

Rotate Image

LeetCode 48 · मध्यम · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

Transpose, then mirror each row.

मध्यमArrayMathMatrix

मी 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).