leetcode-study

54. Spiral Matrix

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        """
        Returns all elements of the input 2D matrix in spiral order.
        
        Parameters:
            matrix (List[List[int]]): A 2D list of integers representing the matrix.
        
        Returns:
            List[int]: A list of integers representing the matrix elements in spiral order.
        """
        # Initialize boundary pointers.
        top = 0  # The top row index of the current spiral layer.
        bottom = len(matrix) - 1  # The bottom row index of the current spiral layer.
        left = 0  # The left column index of the current spiral layer.
        right = len(matrix[0]) - 1  # The right column index of the current spiral layer.
        
        # List to store the spiral order.
        spiral = []
        
        # Continue until all layers are processed.
        while top <= bottom and left <= right:
            # Traverse from left to right across the top row.
            for j in range(left, right + 1):
                spiral.append(matrix[top][j])
            top += 1  # Move the top boundary down after processing the top row.
            
            # Traverse from top to bottom along the right column.
            for i in range(top, bottom + 1):
                spiral.append(matrix[i][right])
            right -= 1  # Move the right boundary left after processing the right column.
            
            # Traverse from right to left across the bottom row if it still exists.
            if top <= bottom:
                for j in range(right, left - 1, -1):
                    spiral.append(matrix[bottom][j])
                bottom -= 1  # Move the bottom boundary up.
            
            # Traverse from bottom to top along the left column if it still exists.
            if left <= right:
                for i in range(bottom, top - 1, -1):
                    spiral.append(matrix[i][left])
                left += 1  # Move the left boundary right.
        
        return spiral

Summary of Techniques and Approaches:

These techniques are generally applicable to problems requiring systematic traversal or partitioning of data structures in varying directions.