{"id":5054,"date":"2019-04-12T23:29:56","date_gmt":"2019-04-13T06:29:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5054"},"modified":"2019-04-12T23:30:53","modified_gmt":"2019-04-13T06:30:53","slug":"leetcode-54-spiral-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-54-spiral-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 54. Spiral Matrix"},"content":{"rendered":"\n<p>Given a matrix of&nbsp;<em>m<\/em>&nbsp;x&nbsp;<em>n<\/em>&nbsp;elements (<em>m<\/em>&nbsp;rows,&nbsp;<em>n<\/em>&nbsp;columns), return all elements of the matrix in spiral order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong>\n[\n [ 1, 2, 3 ],\n [ 4, 5, 6 ],\n [ 7, 8, 9 ]\n]\n<strong>Output:<\/strong> [1,2,3,6,9,8,7,4,5]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong>\n[\n  [1, 2, 3, 4],\n  [5, 6, 7, 8],\n  [9,10,11,12]\n]\n<strong>Output:<\/strong> [1,2,3,4,8,12,11,10,9,5,6,7]<\/pre>\n\n\n\n<p><strong>Solution: Simulation<\/strong><\/p>\n\n\n\n<p>Keep track of the current bounds (left, right, top, bottom).<\/p>\n\n\n\n<p>Init: left = 0, right = n &#8211; 1, top = 0, bottom = m &#8211; 1<\/p>\n\n\n\n<p>Each time we move in one direction and shrink the bounds and turn 90 degrees:<br>1. go right =&gt; &#8211;top<br>2. go down =&gt; &#8211;right<br>3. go left =&gt; ++bottom<br>4. go up =&gt; ++left<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<int> spiralOrder(vector<vector<int>>& matrix) {\n    if (matrix.empty()) return {};\n    int l = 0;\n    int t = 0;\n    int r = matrix[0].size();\n    int b = matrix.size();\n    const int total = (r--) * (b--);      \n    int d = 0;\n    int x = 0;\n    int y = 0;\n    vector<int> ans;\n    while (ans.size() < total - 1) {      \n      if (d == 0) {\n        while (x < r) ans.push_back(matrix[y][x++]);\n        ++t;\n      } else if (d == 1) {\n        while (y < b) ans.push_back(matrix[y++][x]);\n        --r;\n      } else if (d == 2) {\n        while (x > l) ans.push_back(matrix[y][x--]);\n        --b;\n      } else if (d == 3) {\n        while (y > t) ans.push_back(matrix[y--][x]);\n        ++l;\n      }\n      d = (d + 1) % 4;\n    }\n    if (ans.size() != total) ans.push_back(matrix[y][x]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a matrix of&nbsp;m&nbsp;x&nbsp;n&nbsp;elements (m&nbsp;rows,&nbsp;n&nbsp;columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[216,177,179],"class_list":["post-5054","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-matrix","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5054","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=5054"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5054\/revisions"}],"predecessor-version":[{"id":5056,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5054\/revisions\/5056"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}