{"id":2282,"date":"2022-01-23T10:00:11","date_gmt":"2022-01-23T02:00:11","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2282"},"modified":"2022-01-23T10:00:12","modified_gmt":"2022-01-23T02:00:12","slug":"leetcodeday59","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/23\/leetcodeday59\/","title":{"rendered":"leetcodeday59 &#8211;\u87ba\u65cb\u77e9\u9635 II"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570&nbsp;<code>n<\/code>&nbsp;\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b&nbsp;<code>1<\/code>&nbsp;\u5230&nbsp;<code>n<sup>2<\/sup><\/code>&nbsp;\u6240\u6709\u5143\u7d20\uff0c\u4e14\u5143\u7d20\u6309\u987a\u65f6\u9488\u987a\u5e8f\u87ba\u65cb\u6392\u5217\u7684&nbsp;<code>n x n<\/code>&nbsp;\u6b63\u65b9\u5f62\u77e9\u9635&nbsp;<code>matrix<\/code>&nbsp;\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\/spiraln.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>n = 3\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;1,2,3],&#91;8,9,4],&#91;7,6,5]]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;1]]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>1 &lt;= n &lt;= 20<\/code><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=59 lang=python3\r\n#\r\n# &#91;59] \u87ba\u65cb\u77e9\u9635 II\r\n#\r\n\r\n# @lc code=start\r\nfrom calendar import c\r\n\r\n\r\nclass Solution:\r\n    def generateMatrix(self, n: int) -> List&#91;List&#91;int]]:\r\n        rev=&#91;&#91;0]*n for i in range(n)]\r\n        rstart=0\r\n        cstart=0\r\n        rend=n-1\r\n        cend=n-1\r\n        mid=1\r\n        while cend>=cstart and rstart&lt;=rend:\r\n            for l in range(cstart,cend+1):\r\n                rev&#91;rstart]&#91;l]=mid\r\n                mid=mid+1\r\n            #rev.extend(matrix&#91;rstart]&#91;cstart:cend+1])\r\n            for l in range(rstart+1,rend):\r\n                rev&#91;l]&#91;cend]=mid   \r\n                mid=mid+1\r\n            for l in range(cend,cstart-1,-1):\r\n                rev&#91;rend]&#91;l]=mid   \r\n                mid=mid+1\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&#91;l]&#91;cstart]=mid   \r\n                mid=mid+1\r\n            cend=cend-1\r\n            cstart=cstart+1\r\n            rstart=rstart+1\r\n            rend=rend-1\r\n        if n\/\/2!=n\/2:\r\n            rev&#91;n\/\/2]&#91;n\/\/2]=n*n\r\n        return rev\r\n<\/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-269.png\" alt=\"\" class=\"wp-image-2283\" width=\"462\" height=\"211\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-269.png 608w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-269-300x137.png 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570&nbsp;n&nbsp;\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b&nbsp;1&nbsp;\u5230&nbsp;n2&nbsp; &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/23\/leetcodeday59\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday59 &#8211;\u87ba\u65cb\u77e9\u9635 II<\/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\/2282"}],"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=2282"}],"version-history":[{"count":1,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2282\/revisions"}],"predecessor-version":[{"id":2284,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2282\/revisions\/2284"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2282"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}