{"id":2372,"date":"2022-01-25T13:58:43","date_gmt":"2022-01-25T05:58:43","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2372"},"modified":"2022-01-25T13:58:44","modified_gmt":"2022-01-25T05:58:44","slug":"leetcodeday68","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/25\/leetcodeday68\/","title":{"rendered":"leetcodeday68 &#8211;\u6587\u672c\u5de6\u53f3\u5bf9\u9f50"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5355\u8bcd\u6570\u7ec4\u548c\u4e00\u4e2a\u957f\u5ea6&nbsp;<em>maxWidth<\/em>\uff0c\u91cd\u65b0\u6392\u7248\u5355\u8bcd\uff0c\u4f7f\u5176\u6210\u4e3a\u6bcf\u884c\u6070\u597d\u6709&nbsp;<em>maxWidth<\/em>&nbsp;\u4e2a\u5b57\u7b26\uff0c\u4e14\u5de6\u53f3\u4e24\u7aef\u5bf9\u9f50\u7684\u6587\u672c\u3002<\/p>\n\n\n\n<p>\u4f60\u5e94\u8be5\u4f7f\u7528\u201c\u8d2a\u5fc3\u7b97\u6cd5\u201d\u6765\u653e\u7f6e\u7ed9\u5b9a\u7684\u5355\u8bcd\uff1b\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c3d\u53ef\u80fd\u591a\u5730\u5f80\u6bcf\u884c\u4e2d\u653e\u7f6e\u5355\u8bcd\u3002\u5fc5\u8981\u65f6\u53ef\u7528\u7a7a\u683c&nbsp;<code>' '<\/code>&nbsp;\u586b\u5145\uff0c\u4f7f\u5f97\u6bcf\u884c\u6070\u597d\u6709&nbsp;<em>maxWidth<\/em>&nbsp;\u4e2a\u5b57\u7b26\u3002<\/p>\n\n\n\n<p>\u8981\u6c42\u5c3d\u53ef\u80fd\u5747\u5300\u5206\u914d\u5355\u8bcd\u95f4\u7684\u7a7a\u683c\u6570\u91cf\u3002\u5982\u679c\u67d0\u4e00\u884c\u5355\u8bcd\u95f4\u7684\u7a7a\u683c\u4e0d\u80fd\u5747\u5300\u5206\u914d\uff0c\u5219\u5de6\u4fa7\u653e\u7f6e\u7684\u7a7a\u683c\u6570\u8981\u591a\u4e8e\u53f3\u4fa7\u7684\u7a7a\u683c\u6570\u3002<\/p>\n\n\n\n<p>\u6587\u672c\u7684\u6700\u540e\u4e00\u884c\u5e94\u4e3a\u5de6\u5bf9\u9f50\uff0c\u4e14\u5355\u8bcd\u4e4b\u95f4\u4e0d\u63d2\u5165<strong>\u989d\u5916\u7684<\/strong>\u7a7a\u683c\u3002<\/p>\n\n\n\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n\n\n\n<ul><li>\u5355\u8bcd\u662f\u6307\u7531\u975e\u7a7a\u683c\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26\u5e8f\u5217\u3002<\/li><li>\u6bcf\u4e2a\u5355\u8bcd\u7684\u957f\u5ea6\u5927\u4e8e 0\uff0c\u5c0f\u4e8e\u7b49\u4e8e&nbsp;<em>maxWidth<\/em>\u3002<\/li><li>\u8f93\u5165\u5355\u8bcd\u6570\u7ec4&nbsp;<code>words<\/code>&nbsp;\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5355\u8bcd\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong>\nwords = &#91;\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\nmaxWidth = 16\n<strong>\u8f93\u51fa:<\/strong>\n&#91;\n&nbsp; &nbsp;\"This &nbsp; &nbsp;is &nbsp; &nbsp;an\",\n&nbsp; &nbsp;\"example &nbsp;of text\",\n&nbsp; &nbsp;\"justification. &nbsp;\"\n]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong>\nwords = &#91;\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\nmaxWidth = 16\n<strong>\u8f93\u51fa:<\/strong>\n&#91;\n&nbsp; \"What &nbsp; must &nbsp; be\",\n&nbsp; \"acknowledgment &nbsp;\",\n&nbsp; \"shall be &nbsp; &nbsp; &nbsp; &nbsp;\"\n]\n<strong>\u89e3\u91ca: <\/strong>\u6ce8\u610f\u6700\u540e\u4e00\u884c\u7684\u683c\u5f0f\u5e94\u4e3a \"shall be    \" \u800c\u4e0d\u662f \"shall     be\",\n&nbsp;    \u56e0\u4e3a\u6700\u540e\u4e00\u884c\u5e94\u4e3a\u5de6\u5bf9\u9f50\uff0c\u800c\u4e0d\u662f\u5de6\u53f3\u4e24\u7aef\u5bf9\u9f50\u3002       \n     \u7b2c\u4e8c\u884c\u540c\u6837\u4e3a\u5de6\u5bf9\u9f50\uff0c\u8fd9\u662f\u56e0\u4e3a\u8fd9\u884c\u53ea\u5305\u542b\u4e00\u4e2a\u5355\u8bcd\u3002<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong>\nwords = &#91;\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n&nbsp;        \"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\nmaxWidth = 20\n<strong>\u8f93\u51fa:<\/strong>\n&#91;\n&nbsp; \"Science &nbsp;is &nbsp;what we\",\n  \"understand &nbsp; &nbsp; &nbsp;well\",\n&nbsp; \"enough to explain to\",\n&nbsp; \"a &nbsp;computer. &nbsp;Art is\",\n&nbsp; \"everything &nbsp;else &nbsp;we\",\n&nbsp; \"do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"\n]<\/code><\/pre>\n\n\n\n<p>code\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># &#91;68] \u6587\u672c\u5de6\u53f3\u5bf9\u9f50\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def fullJustify(self, words: List&#91;str], maxWidth: int) -> List&#91;str]:\r\n        lens=len(words)\r\n        rev=list()\r\n        nowlens=0\r\n        start=0\r\n        i=0\r\n        while i&lt;lens:\r\n            wordlen=len(words&#91;i])\r\n            nowlens=nowlens+wordlen\r\n            #print(i,nowlens)\r\n            if nowlens&lt;maxWidth:\r\n                nowlens+=1\r\n                i=i+1\r\n                #print(i)\r\n                #print(nowlens)\r\n            elif nowlens>maxWidth:\r\n                nowlens=nowlens-wordlen-1\r\n                \r\n                reslens=maxWidth-nowlens\r\n                m=(i-1-start)\r\n                if m==0:\r\n                    mid=\"\"\r\n                    mid=mid+words&#91;start]+(maxWidth-len(words&#91;start]))*\" \"\r\n                    rev.append(mid)\r\n                #print(reslens,reslens\/\/(i-1-start),reslens\/(i-1-start))\r\n                elif reslens\/\/(i-1-start)==reslens\/(i-1-start):\r\n                    mid=\"\"\r\n                    x=reslens\/\/(i-1-start)+1\r\n                    for j in range(start,i-1):\r\n                        mid=mid+words&#91;j]+x*\" \"\r\n                    mid=mid+words&#91;i-1]\r\n                    rev.append(mid)\r\n                else:\r\n                    other=reslens%(i-1-start)\r\n                    mid=\"\"\r\n                    x=reslens\/\/(i-1-start)+1\r\n                    print(x,other)\r\n                    for j in range(start,i-1):\r\n                        if other>0:\r\n                            mid=mid+words&#91;j]+x*\" \"+\" \"\r\n                            other-=1\r\n                        else:\r\n                            mid=mid+words&#91;j]+x*\" \"\r\n                    mid=mid+words&#91;i-1]\r\n                    rev.append(mid)\r\n                nowlens=0\r\n                start=i\r\n            elif nowlens==maxWidth:\r\n                mid=\"\"\r\n                for j in range(start,i):\r\n                    mid=mid+words&#91;j]+\" \"\r\n                mid=mid+words&#91;i]\r\n                rev.append(mid)\r\n                start=i+1\r\n                nowlens=0\r\n                i=i+1\r\n        if start&lt;lens:\r\n            mid=\"\"\r\n            for j in range(start,lens):\r\n                mid=mid+words&#91;j]+\" \"\r\n            mid=mid&#91;:-1]\r\n            other=maxWidth-len(mid)\r\n            mid=mid+other*\" \"\r\n            rev.append(mid)\r\n        return rev    \r\n                \r\n             \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-288.png\" alt=\"\" class=\"wp-image-2374\" width=\"454\" height=\"120\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-288.png 856w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-288-300x80.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-288-768x204.png 768w\" sizes=\"(max-width: 454px) 100vw, 454px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5355\u8bcd\u6570\u7ec4\u548c\u4e00\u4e2a\u957f\u5ea6&nbsp;maxWidth\uff0c\u91cd\u65b0\u6392\u7248\u5355\u8bcd\uff0c\u4f7f\u5176\u6210\u4e3a\u6bcf\u884c\u6070\u597d\u6709&nbsp;maxWi &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/25\/leetcodeday68\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday68 &#8211;\u6587\u672c\u5de6\u53f3\u5bf9\u9f50<\/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\/2372"}],"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=2372"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2372\/revisions"}],"predecessor-version":[{"id":2375,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2372\/revisions\/2375"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2372"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}