Solving LeetCode Problem 54: Spiral Matrix

Problem Description

Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

Constraints:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

Solution


public class _054_SpiralMatrix {
    public List<Integer> spiralOrder(int[][] matrix) {
        var list = new ArrayList<Integer>();
        if (matrix.length == 0 || matrix[0].length == 0)
            return list;

        int x = 0, y = 0;
        int minX = 0, maxX = matrix[0].length - 1;
        int minY = 0, maxY = matrix.length - 1;

        var direction = Direction.Right;
        while (list.size() < matrix.length * matrix[0].length) {
            list.add(matrix[y][x]);
            switch (direction) {
                case Right:
                    if (x < maxX) {
                        x++;
                    } else {
                        direction = Direction.Down;
                        y++;
                        maxX--;
                    }
                    break;
                case Down:
                    if (y < maxY) {
                        y++;
                    } else {
                        direction = Direction.Left;
                        x--;
                        maxY--;
                    }
                    break;
                case Left:
                    if (x > minX) {
                        x--;
                    } else {
                        direction = Direction.Up;
                        y--;
                        minX++;
                    }
                    break;
                case Up:
                    if (y > minY + 1) {
                        y--;
                    } else {
                        direction = Direction.Right;
                        x++;
                        minY++;
                    }
                    break;
            }
        }
        return list;
    }

    private enum Direction {
        Up,
        Down,
        Left,
        Right
    }
}

In this YouTube video, I was explained how to solve this problem step by step.