> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_54_spiral_matrix.md).

# Problem 54: Spiral Matrix

> <https://leetcode.com/problems/spiral-matrix/>

## 思路

![](/files/-Lpv9zXOsg0PcXPeVZKj)

* x,y 控制方向，m, n 控制 boundary

```java
public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> rst = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0) {
            return rst;
        }

        int m = matrix.length;
        int n = matrix[0].length;
        int x = 0, y = 0;

        while (m > 0 && n > 0) {
            if (m == 1) {
                for (int i = 0; i < n; i++) {
                    rst.add(matrix[x][y++]);
                }
                break;
            } else if (n == 1) {
                for (int i = 0; i < m; i++) {
                    rst.add(matrix[x++][y]);
                }
                break;
            }

            // top left to right
            for (int i = 0; i < n - 1; i++) {
                rst.add(matrix[x][y++]);
            }
            // to bottom
            for (int i = 0; i < m - 1; i++) {
                rst.add(matrix[x++][y]);
            }
            // to left
            for (int i = 0; i < n - 1; i++) {
                rst.add(matrix[x][y--]);
            }
            // to top
            for (int i = 0; i < m - 1; i++) {
                rst.add(matrix[x--][y]);
            }

            // next cycle
            x++;
            y++;
            m -= 2;
            n -= 2;
        }

        return rst;
    }
}
```

## 易错点

1. 进入 next cycle 的时候，x++ &#x20;

   因为上一步的时候，`rst.add(matrix[x--][y]);`，x 多退了一步，现在要退回来。
2. 考虑 m 和 n 为 1 的情况。
3. m 和 n 的 step 是 2, 因为走完一圈以后，左右两边各有一个元素占据，新的 boundary 少了 2


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_54_spiral_matrix.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
