# 螺旋矩阵

<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ，请按照 <strong>顺时针螺旋顺序</strong> ，返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/0d476dcc094b85563f4f43c393ef1425.jpeg" style="width: 242px; height: 242px;" /><pre><strong>输入：</strong>matrix = [[1,2,3],[4,5,6],[7,8,9]]<strong><br />输出：</strong>[1,2,3,6,9,8,7,4,5]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/1c83ff2d8f7cc194b08715bacdedff99.jpeg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]<strong><br />输出：</strong>[1,2,3,4,8,12,11,10,9,5,6,7]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>m == matrix.length</code></li>	<li><code>n == matrix[i].length</code></li>	<li><code>1 <= m, n <= 10</code></li>	<li><code>-100 <= matrix[i][j] <= 100</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        vector<int> ans;
        if (matrix.size() == 0)
            return ans;
        int cir = 0;
        int row = matrix.size();
        int col = matrix[0].size();
        int max_cir = int(min(matrix.size(), matrix[0].size()) + 1) / 2;
        for (; cir < max_cir; cir++)
        {
            for (int i = cir; i < col - cir; i++)
            {
                ans.push_back(matrix[cir][i]);
            }

            for (int i = cir + 1; i < row - cir; i++)
            {
                ans.push_back(matrix[i][col - 1 - cir]);
            }
            __________________;
            for (int i = row - 2 - cir; i > cir && (col - 1 - cir != cir); i--)
            {
                ans.push_back(matrix[i][cir]);
            }
        }
        return ans;
    }
};
```

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        vector<int> ans;
        if (matrix.size() == 0)
            return ans;
        int cir = 0;
        int row = matrix.size();
        int col = matrix[0].size();
        int max_cir = int(min(matrix.size(), matrix[0].size()) + 1) / 2;
        for (; cir < max_cir; cir++)
        {
            for (int i = cir; i < col - cir; i++)
            {
                ans.push_back(matrix[cir][i]);
            }

            for (int i = cir + 1; i < row - cir; i++)
            {
                ans.push_back(matrix[i][col - 1 - cir]);
            }
            for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--)
            {
                ans.push_back(matrix[row - cir - 1][i]);
            }
            for (int i = row - 2 - cir; i > cir && (col - 1 - cir != cir); i--)
            {
                ans.push_back(matrix[i][cir]);
            }
        }
        return ans;
    }
};
```

## 答案

```cpp
for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--)
{
	ans.push_back(matrix[row - cir - 1][i]);
}
```

## 选项

### A

```cpp
for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--)
{
	ans.push_back(matrix[row - cir + 1][i]);
}
```

### B

```cpp
for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--)
{
	ans.push_back(matrix[row - cir][i]);
}
```

### C

```cpp
for (int i = col - 2 - cir;row - 1 - cir != cir; i--)
{
	ans.push_back(matrix[row - cir + 1][i]);
}
```