Link: 54. Spiral Matrix - Medium
Track: Amazon Tag

Question

Restate

Edge Case


Method 1 - Boundary Simulation
Method 2

Method 1 - Boundary Simulation

Approach

Complexity

  • Time Complexity: O()
  • Space Complexity: O(1) extra (excluding the output list)

Code

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        rows = len(matrix)
        cols = len(matrix[0])
        top, bottom = 0, rows - 1 # 2
        left, right = 0, cols - 1
 
        res = []
        while top <= bottom and left <= right: # 0, 2
            for col in range(left, right + 1):
                res.append(matrix[top][col])
            top += 1
            
            for row in range(top, bottom + 1): 
                res.append(matrix[row][right]) #
            right -= 1
 
            if top <= bottom:
                for col in range(right, left - 1, -1):
                    res.append(matrix[bottom][col])
                bottom -= 1
 
            if left <= right:
                for row in range(bottom, top - 1, -1):
                    res.append(matrix[row][left])
                left += 1
        
        return res
 

History

  • Feb-19-2026 Peeked