{"id":3020,"date":"2020-02-24T13:24:20","date_gmt":"2020-02-24T05:24:20","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3020"},"modified":"2020-02-24T13:24:20","modified_gmt":"2020-02-24T05:24:20","slug":"%e3%80%90leetcode%e3%80%910068-%e6%96%87%e6%9c%ac%e5%b7%a6%e5%8f%b3%e5%af%b9%e9%bd%90","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/3020\/","title":{"rendered":"\u3010LeetCode\u30110068 \u6587\u672c\u5de6\u53f3\u5bf9\u9f50"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/text-justification\/\">https:\/\/leetcode-cn.com\/problems\/text-justification\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def fullJustify(self, words, maxWidth):\n        <em>\"\"\"\n        :type words: List[str]\n        :type maxWidth: int\n        :rtype: List[str]\n        \"\"\"\n        <\/em>def find_one_line(word_idx):\n            <em>\"\"\"\n            \u5bfb\u627e\u53ef\u4ee5\u653e\u5728\u4e00\u884c\u4e2d\u7684\u5355\u8bcd\n            <\/em><strong><em>:param<\/em><\/strong><em> word_idx:\n            <\/em><strong><em>:return<\/em><\/strong><em>:\n            \"\"\"\n            <\/em>current_length = len(words[word_idx])\n            temp_str = words[word_idx]\n            # temp_list = [words[word_idx]]\n            word_idx += 1\n            if word_idx >= len(words):\n                res_list.append(temp_str)\n                return\n            # \u6311\u9009\u5355\u8bcd\n            while current_length + len(words[word_idx]) + 1 &lt;= maxWidth:\n                temp_str = temp_str + ' ' + words[word_idx]\n                # temp_list.append(' ' + words[word_idx])\n                word_idx += 1\n                if word_idx >= len(words):\n                    break\n                # current_length += len(words[word_idx])+1\n                current_length = len(temp_str)\n            res_list.append(temp_str)\n            if word_idx &lt; len(words):\n                find_one_line(word_idx)\n        def check_space():\n            <em>\"\"\"\n            \u68c0\u67e5\u5904\u7406\u7a7a\u683c\n            <\/em><strong><em>:return<\/em><\/strong><em>:\n            \"\"\"\n            <\/em># \u5904\u7406\u524dn\u884c\n            for i in range(len(res_list) - 1):\n                current_length = len(res_list[i])\n                space_length = maxWidth - current_length\n                temp_cache = res_list[i].split(' ')\n                if len(temp_cache) - 1 > 0:\n                    m, n = divmod(space_length, len(temp_cache) - 1)\n                    for j in range(n):\n                        temp_cache[j] = temp_cache[j] + ' ' * (m + 1 + 1)\n                    for j in range(n, len(temp_cache) - 1):\n                        temp_cache[j] = temp_cache[j] + ' ' * (m + 1)\n                else:\n                    if space_length != 0:\n                        m = 0\n                        n = space_length - 1\n                        temp_cache[0] = temp_cache[0] + ' ' * (m + n + 1)\n                    else:\n                            pass\n                res_str = ''\n                for j in range(len(temp_cache)):\n                    res_str = res_str + temp_cache[j]\n                res.append(res_str)\n            # \u5904\u7406\u6700\u540e\u4e00\u884c\n            current_length = len(res_list[-1])\n            space_length = maxWidth - current_length\n            temp_cache = res_list[-1].split(' ')\n            for i in range(len(temp_cache) - 1):\n                temp_cache[i] = temp_cache[i] + ' '\n            temp_cache[-1] = temp_cache[-1] + ' ' * space_length\n            res_str = ''\n            for j in range(len(temp_cache)):\n                res_str = res_str + temp_cache[j]\n            res.append(res_str)\n        res_list = []\n        res = []\n        find_one_line(0)\n        check_space()\n        return res\nif __name__ == '__main__':\n    words = [\"What\", \"must\", \"be\", \"acknowledgment\", \"shall\", \"be\"]\n    words = [\"Science\", \"is\", \"what\", \"we\", \"understand\", \"well\", \"enough\", \"to\", \"explain\", \"to\", \"a\", \"computer.\",\n             \"Art\", \"is\", \"everything\", \"else\", \"we\", \"do\"]\n    words = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\n    words = [\"Listen\", \"to\", \"many,\", \"speak\", \"to\", \"a\", \"few.\"]\n    maxWidth = 6\n    print(Solution().fullJustify(words, maxWidth))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u8fd9\u9053\u9898\u9996\u5148\u7b5b\u9009\u80fd\u5728\u4e00\u884c\u4e2d\u51fa\u73b0\u7684\u8bcd\uff0c\u4e5f\u5c31\u662f\u5224\u65ad\u5982\u679c\u52a0\u4e0a\u8fd9\u4e2a\u8bcd\u4f1a\u4e0d\u4f1a\u8d85\u51famaxWidth\uff0c\u7136\u540e\u6839\u636e\u5f53\u524d\u884c\u7684\u957f\u5ea6\u6765\u8ba1\u7b97\u9700\u8981\u63d2\u5165\u591a\u5c11\u4e2a\u7a7a\u683c\uff0c\u4f9d\u6b21\u63d2\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/text-justificatio [&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":1667,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3020"}],"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=3020"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3020\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3020"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}