{"id":2947,"date":"2020-02-17T12:41:21","date_gmt":"2020-02-17T04:41:21","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2947"},"modified":"2020-02-17T12:41:21","modified_gmt":"2020-02-17T04:41:21","slug":"%e3%80%90leetcode%e3%80%910054-%e8%9e%ba%e6%97%8b%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2947\/","title":{"rendered":"\u3010LeetCode\u3011*0054 \u87ba\u65cb\u77e9\u9635"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/spiral-matrix\/\">https:\/\/leetcode-cn.com\/problems\/spiral-matrix\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def spiralOrder(self, matrix):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> matrix: List[List[int]]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[int]\n        \"\"\"\n        <\/em>ret = []\n        if matrix == []:\n            return ret\n        ret.extend(matrix[0])  # \u4e0a\u4fa7\n        # \u751f\u6210\u4e00\u4e2a\u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u6bd4\u59821\uff0c2\uff0c3\u751f\u62103 2 1\n        new = [reversed(i) for i in matrix[1:]]\n        if new == []:\n            return ret\n        # zip\u7528\u6765\u89e3\u538b\u8fed\u4ee3\u5668 \u76f8\u5f53\u4e8e\u628anew\u751f\u6210\u7684\u8fed\u4ee3\u5668\u4f9d\u6b21\u904d\u5386\uff0c\u8fbe\u5230\u4e00\u4e2a\u7c7b\u4f3c\u201c\u9006\u65f6\u9488\u65cb\u8f6c\u201d\u7684\u6837\u5b50\u3002\n        r = self.spiralOrder([i for i in zip(*new)])\n        ret.extend(r)\n        return ret\nif __name__ == '__main__':\n    matrix = [\n        [1, 2, 3],\n        [4, 5, 6],\n        [7, 8, 9]\n    ]\n    matrix = [\n        [1, 2, 3, 4],\n        [5, 6, 7, 8],\n        [9, 10, 11, 12]\n    ]\n    print(Solution().spiralOrder(matrix))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u521a\u4e00\u5f00\u59cb\u6211\u662f\u6253\u7b97\u7c7b\u4f3c\u4e8e\u524d\u9762\u6709\u4e00\u9053\u9898Z\u5b57\u6296\u52a8\u4e00\u6837\uff0c\u9760\u4e0b\u6807\u627e\u7ed3\u679c\uff0c\u4f46\u662f\u53d1\u73b0\u4e00\u4e2a\u8fd9\u4e2a\u505a\u6cd5\uff0c\u5c31\u771f\u7684\u5f3a\uff0c\u5199\u4e0b\u6765\u5b66\u4e60\u4e00\u4e0b\u3002<\/p>\n\n\n<p>\u56e0\u4e3a\u662f\u987a\u65f6\u9488\u627e\u5143\u7d20\uff0c\u76f8\u5f53\u4e8e\u6211\u4eec\u4e0d\u65ad\u7684\u53d6\u77e9\u9635\u7b2c\u4e00\u884c\u7136\u540e\u9006\u65f6\u9488\u65cb\u8f6c\u77e9\u9635\uff0c\u56e0\u6b64\u8fd9\u4e2a\u65b9\u6cd5\u91c7\u7528\u5bf9\u77e9\u9635\uff08\u4e8c\u7ef4\u5217\u8868\uff09\u8fdb\u884c\u64cd\u4f5c\u3002\u9996\u5148\u53d6\u7b2c\u4e00\u884c\uff0c\u7136\u540e\u751f\u6210\u4e00\u4e2a\u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u6700\u540e\u7531zip(*)\u5904\u7406\u8fd9\u4e2a\u8fed\u4ee3\u5668\uff0c\u8fbe\u5230\u4e86\u65cb\u8f6c\u7684\u6548\u679c\u3002<\/p>\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">[<br \/>        [1, 2, 3, 4],<br \/>        [5, 6, 7, 8],<br \/>        [9, 10, 11, 12]<br \/>    ]<\/pre>\n\n\n<p>\u8fd9\u4e2a\u77e9\u9635\u9996\u5148\u6211\u4eec\u53d6\u4e86\u7b2c\u4e00\u884c\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">[\n        [5, 6, 7, 8],\n        [9, 10, 11, 12]\n    ]<\/pre>\n\n\n<p>\u8fd9\u65f6\u5019\u751f\u6210\u4e00\u4e2a\u53cd\u5411\u8fed\u4ee3\u5668\u5217\u8868\uff0c\u5728\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4e2d\u4f60\u53ef\u4ee5\u7406\u89e3\u6210\u8fd9\u6837\uff0c\u53ea\u4e0d\u8fc7\u5b83\u8fd8\u662f\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u4e0d\u662f\u5217\u8868<\/p>\n\n\n<pre class=\"wp-block-preformatted\">[\n        [8, 7, 6, 8],\n        [12, 11, 10, 9]\n    ]<\/pre>\n\n\n<p>\u6700\u540e\u6211\u4eec\u901a\u8fc7zip\uff08*\uff09\u904d\u5386\u8fd9\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">[\n        [8\uff0c12],\n        [7\uff0c11],\n        [6\uff0c10],\n        [8\uff0c9]\n    ]<\/pre>\n\n\n<p>\u8fd9\u65f6\u5019\u6211\u4eec\u518d\u53d6\u7b2c\u4e00\u884c\uff0c\u662f\u4e0d\u662f\u5c31\u6b65\u5165\u6b63\u8f68\u4e86\uff1f<\/p>\n\n\n<p>\u8fd9\u9053\u9898\u5b66\u5230\u4e86\u4e0d\u5c11\uff0c\u9996\u5148\u8fd9\u4e2a\u601d\u8def\u6211\u662f\u6ca1\u60f3\u5230\u7684\uff0c\u518d\u4e00\u4e2a\u662freversed()\u8fd9\u4e2a\u53cd\u5411\u8fed\u4ee3\u5668\u548czip(*)\u6211\u8fd8\u662f\u7b2c\u4e00\u6b21\u9047\u5230\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/spiral-matrix\/ #  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2059,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2947"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2947"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2947\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}