{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "03ce5873e9874d519a639d13cbafda4f",
  "keywords": "数组,矩阵,模拟",
  "title": "螺旋矩阵 II",
  "desc": [
    {
      "content": "\n<p>给你一个正整数 <code>n</code> ，生成一个包含 <code>1</code> 到 <code>n<sup>2</sup></code> 所有元素，且元素按顺时针顺序螺旋排列的 <code>n x n</code> 正方形矩阵 <code>matrix</code> 。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0059.Spiral%20Matrix%20II/images/spiraln.jpg\" style=\"width: 242px; height: 242px;\" /><pre><strong>输入：</strong>n = 3<strong><br />输出：</strong>[[1,2,3],[8,9,4],[7,6,5]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>n = 1<strong><br />输出：</strong>[[1]]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>1 <= n <= 20</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<vector<int>> generateMatrix(int n)\n\t{\n\t\tvector<vector<int>> matrix(n, vector<int>(n));\n\t\tint direction = 0;\n\t\tint hor_top = 0;\n\t\tint hor_bottom = n - 1;\n\t\tint ver_left = 0;\n\t\tint ver_right = n - 1;\n\t\tint num = 0;\n\t\twhile (num < n * n)\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\tmatrix[hor_top][i] = ++num;\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\tmatrix[i][ver_right] = ++num;\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\tmatrix[hor_bottom][i] = ++num;\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\tmatrix[i][ver_left] = ++num;\n\t\t\t\t}\n\t\t\t\tver_left++;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdirection++;\n\t\t\tdirection %= 4;\n\t\t}\n\t\treturn matrix;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-93f92073f6254a549ac8e0010c008337",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470226"
}