{"id":2263,"date":"2022-01-22T15:10:03","date_gmt":"2022-01-22T07:10:03","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2263"},"modified":"2022-01-22T15:10:05","modified_gmt":"2022-01-22T07:10:05","slug":"leetcodeday54","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/22\/leetcodeday54\/","title":{"rendered":"leetcodeday54 -\u87ba\u65cb\u77e9\u9635"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;<code>m<\/code>&nbsp;\u884c&nbsp;<code>n<\/code>&nbsp;\u5217\u7684\u77e9\u9635&nbsp;<code>matrix<\/code>&nbsp;\uff0c\u8bf7\u6309\u7167&nbsp;<strong>\u987a\u65f6\u9488\u87ba\u65cb\u987a\u5e8f<\/strong>&nbsp;\uff0c\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/13\/spiral1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>matrix = &#91;&#91;1,2,3],&#91;4,5,6],&#91;7,8,9]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;1,2,3,6,9,8,7,4,5]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/11\/13\/spiral.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>matrix = &#91;&#91;1,2,3,4],&#91;5,6,7,8],&#91;9,10,11,12]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;1,2,3,4,8,12,11,10,9,5,6,7]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>m == matrix.length<\/code><\/li><li><code>n == matrix[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 10<\/code><\/li><li><code>-100 &lt;= matrix[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=54 lang=python3\r\n#\r\n# &#91;54] \u87ba\u65cb\u77e9\u9635\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def spiralOrder(self, matrix: List&#91;List&#91;int]]) -> List&#91;int]:\r\n        rstart=0\r\n        cstart=0\r\n        rend=len(matrix)-1\r\n        cend=len(matrix&#91;0])-1\r\n        rev=list()\r\n        if cstart==cend:\r\n            for i in matrix:\r\n                rev.extend(i) \r\n            return rev\r\n        if  rstart==rend:\r\n            return matrix&#91;0]\r\n        while rstart&lt;rend and cstart&lt;cend :\r\n            print(rstart,rend,cstart,cend)\r\n            for l in range(cstart,cend+1):\r\n                rev.append(matrix&#91;rstart]&#91;l])    \r\n            #rev.extend(matrix&#91;rstart]&#91;cstart:cend+1])\r\n            for l in range(rstart+1,rend):\r\n                rev.append(matrix&#91;l]&#91;cend])\r\n            for l in range(cend,cstart-1,-1):\r\n                rev.append(matrix&#91;rend]&#91;l])\r\n            #rev.extend(matrix&#91;rend]&#91;cstart:cend+1]&#91;::-1])\r\n            for l in range(rend-1,rstart,-1):\r\n                rev.append(matrix&#91;l]&#91;cstart])\r\n            #print(rev)\r\n            cend=cend-1\r\n            cstart=cstart+1\r\n            rstart=rstart+1\r\n            rend=rend-1\r\n        if rstart==rend and cstart!=cend:\r\n            print(\"fff\")\r\n            for l in range(cstart,cend+1):\r\n                #print(matrix&#91;rstart]&#91;l])\r\n                rev.append(matrix&#91;rstart]&#91;l]) \r\n        elif cstart==cend and rstart!=rend:\r\n            for l in range(rstart,rend+1):\r\n                #print(matrix&#91;l]&#91;cstart])\r\n                rev.append(matrix&#91;l]&#91;cstart]) \r\n        elif rstart==rend and cstart==cend:\r\n            rev.append(matrix&#91;rstart]&#91;cstart]) \r\n        return rev\r\n\r\n# @lc code=end<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-266.png\" alt=\"\" class=\"wp-image-2264\" width=\"458\" height=\"177\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-266.png 645w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-266-300x116.png 300w\" sizes=\"(max-width: 458px) 100vw, 458px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;m&nbsp;\u884c&nbsp;n&nbsp;\u5217\u7684\u77e9\u9635&nbsp;matrix&nbsp;\uff0c\u8bf7 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/22\/leetcodeday54\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday54 -\u87ba\u65cb\u77e9\u9635<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2263"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2263"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2263\/revisions"}],"predecessor-version":[{"id":2265,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2263\/revisions\/2265"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}