{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "0f48bea424f74cc99e0a9923a379de7b",
  "keywords": "数组,矩阵,模拟",
  "title": "螺旋矩阵",
  "desc": [
    {
      "content": "\n<p>给你一个 <code>m</code> 行 <code>n</code> 列的矩阵 <code>matrix</code> ，请按照 <strong>顺时针螺旋顺序</strong> ，返回矩阵中的所有元素。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral1.jpg\" 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://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0054.Spiral%20Matrix/images/spiral.jpg\" 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>\t<li><code>m == matrix.length</code></li>\t<li><code>n == matrix[i].length</code></li>\t<li><code>1 <= m, n <= 10</code></li>\t<li><code>-100 <= matrix[i][j] <= 100</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tvector<int> spiralOrder(vector<vector<int>> &matrix)\n\t{\n\t\tvector<int> res;\n\t\tint hor_top = 0;\n\t\tint hor_bottom = matrix.size() - 1;\n\t\tint ver_left = 0;\n\t\tint ver_right = matrix[0].size() - 1;\n\t\tint direction = 0;\n\t\twhile (hor_top <= hor_bottom && ver_left <= ver_right)\n\t\t{\n\t\t\tswitch (direction)\n\t\t\t{\n\t\t\tcase 0:\n\t\t\t\tfor (int i = ver_left; i <= ver_right; i++)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[hor_top][i]);\n\t\t\t\t}\n\t\t\t\thor_top++;\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (int i = hor_top; i <= hor_bottom; i++)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[i][ver_right]);\n\t\t\t\t}\n\t\t\t\tver_right--;\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tfor (int i = ver_right; i >= ver_left; i--)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[hor_bottom][i]);\n\t\t\t\t}\n\t\t\t\thor_bottom--;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tfor (int i = hor_bottom; i >= hor_top; i--)\n\t\t\t\t{\n\t\t\t\t\tres.push_back(matrix[i][ver_left]);\n\t\t\t\t}\n\t\t\t\tver_left++;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdirection++;\n\t\t\tdirection %= 4;\n\t\t}\n\t\treturn res;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-1b393b30506f43f6874f85036d16a3de",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470121"
}