leetcode-study

48. Rotate Image

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        """
        Rotate the given n x n matrix 90 degrees clockwise in-place.
        
        Parameters:
            matrix (List[List[int]]): A 2D list representing an n x n matrix.
            
        Returns:
            None: The function modifies the matrix in-place.
        """
        n = len(matrix)  # 'n' is the size (number of rows/columns) of the square matrix.
        
        # Step 1: Transpose the matrix.
        # Transposing converts rows to columns.
        for i in range(n):
            for j in range(i + 1, n):
                # Swap the elements at positions (i, j) and (j, i)
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        
        # Step 2: Reverse each row.
        # Reversing each row yields the final rotated matrix.
        for i in range(n):
            # Reverse the current row in place.
            matrix[i].reverse()

Summary of Techniques and Approaches:

These approaches can be identified and applied when dealing with problems that require transformation, rearrangement, or optimization of data structures in a memory- and time-efficient way.