{"id":5607,"date":"2019-09-29T20:13:27","date_gmt":"2019-09-30T03:13:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5607"},"modified":"2019-09-29T20:13:43","modified_gmt":"2019-09-30T03:13:43","slug":"leetcode-59-spiral-matrix-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-59-spiral-matrix-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 59. Spiral Matrix II"},"content":{"rendered":"\n<p>Given a positive integer&nbsp;<em>n<\/em>, generate a square matrix filled with elements from 1 to&nbsp;<em>n<\/em><sup>2<\/sup>&nbsp;in spiral order.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 3\n<strong>Output:<\/strong>\n[\n [ 1, 2, 3 ],\n [ 8, 9, 4 ],\n [ 7, 6, 5 ]\n]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int> > generateMatrix(int n) {\n    vector<vector<int>> ans(n, vector<int>(n));\n    int dir = 2, x = 0, y = 0;\n    int max_x = n - 1, max_y = n - 1;\n    int min_x = 0, min_y = 1;\n    int i = 0;\n\n    while (++i <= n*n) {\n      ans[y][x] = i;\n      switch (dir) {\n        \/\/ up\n        case 1: if (--y == min_y) { dir = 2; ++min_y; } break;\n        \/\/ right\n        case 2: if (++x == max_x) { dir = 3; --max_x; } break;\n        \/\/ down\n        case 3: if (++y == max_y) { dir = 4; --max_y; } break;\n        \/\/ left\n        case 4: if (--x == min_x) { dir = 1; ++min_x; } break;\n      }\n    }\n\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer&nbsp;n, generate a square matrix filled with elements from 1 to&nbsp;n2&nbsp;in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[216,177,498,179],"class_list":["post-5607","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-matrix","tag-medium","tag-on2","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5607"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5607\/revisions"}],"predecessor-version":[{"id":5609,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5607\/revisions\/5609"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}