LeetCode 54: Spiral Matrix Solution

Master LeetCode problem 54 (Spiral Matrix), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

54. Spiral Matrix

Problem Explanation

Explanation:

To traverse the matrix in a spiral order, we can repeatedly extract the outermost ring of the matrix until the matrix is empty. We keep track of the boundaries of the current ring and move in four directions (right, down, left, up) while adding elements to the result list.

  • Algorithmic Idea:

    1. Initialize four variables to keep track of the boundaries of the current ring: top, bottom, left, right.
    2. Traverse the matrix in a spiral order by moving right, down, left, and up, while updating the boundaries and adding elements to the result list.
    3. Repeat the process until the matrix is empty.
  • Time Complexity: O(m * n) where m is the number of rows and n is the number of columns in the matrix.

  • Space Complexity: O(1) excluding the result list.

:

Solution Code

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return result;
        }
        
        int top = 0, bottom = matrix.length - 1;
        int left = 0, right = matrix[0].length - 1;
        
        while (top <= bottom && left <= right) {
            for (int i = left; i <= right; i++) {
                result.add(matrix[top][i]);
            }
            top++;
            
            for (int i = top; i <= bottom; i++) {
                result.add(matrix[i][right]);
            }
            right--;
            
            if (top <= bottom) {
                for (int i = right; i >= left; i--) {
                    result.add(matrix[bottom][i]);
                }
                bottom--;
            }
            
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result.add(matrix[i][left]);
                }
                left++;
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 54 (Spiral Matrix)?

This page provides optimized solutions for LeetCode problem 54 (Spiral Matrix) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 54 (Spiral Matrix)?

The time complexity for LeetCode 54 (Spiral Matrix) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 54 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 54 in Java, C++, or Python.

Back to LeetCode Solutions