{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "b29c970537ca4755b87b125bea842657",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef generateMatrix(self, n):\n\t\t\"\"\"\n\t\t:type n: int\n\t\t:rtype: List[List[int]]\n\t\t\"\"\"\n\t\tres = [[0] * n for _ in range(n)]\n\t\tpos = [0, 0]\n\t\tmove = (0, 1)\n\t\tfor index in range(1, n * n + 1):\n\t\t\tres[pos[0]][pos[1]] = index\n\t\t\tif res[(pos[0] + move[0]) % n][(pos[1] + move[1]) % n] > 0:\n\t\t\t\tmove = (move[1], -1 * move[0])\n\t\t\tpos[0] = pos[0] + move[0]\n\t\t\tpos[1] = pos[1] + move[1]\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.generateMatrix(2))",
    "language": "python"
  },
  "node_id": "dailycode-6248a779002b4454b3493e7553b99d49",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470226"
}