{ "question_id": 68, "question_title": "文本左右对齐", "difficulty": "困难", "question_content": "
\n

给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

\n\n

你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。\n

\n\n

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

\n\n

文本的最后一行应为左对齐,且单词之间不插入额外的空格。

\n\n

说明:

\n\n \n\n

示例:

\n\n
输入:\n    words = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\n    maxWidth = 16\n输出:\n    [\n       \"This    is    an\",\n       \"example  of text\",\n       \"justification.  \"\n    ]\n    
\n\n

示例 2:

\n\n
输入:\n    words = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\n    maxWidth = 16\n输出:\n    [\n      \"What   must   be\",\n      \"acknowledgment  \",\n      \"shall be        \"\n    ]\n解释: 注意最后一行的格式应为 \"shall be    \" 而不是 \"shall     be\"\n    因为最后一行应为左对齐,而不是左右两端对齐,第二行同样为左对齐,这是因为这行只包含一个单词。\n    
\n\n

示例 3:

\n\n
输入:\n    words = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n             \"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\n    maxWidth = 20\n输出:\n    [\n      \"Science  is  what we\",\n      \"understand      well\",\n      \"enough to explain to\",\n      \"a  computer.  Art is\",\n      \"everything  else  we\",\n      \"do                  \"\n    ]\n    
\n
", "topic_link": "https://bbs.csdn.net/topics/600470798", "cpp": "#include \n#include \n#include \nstatic void line_fill(char *line, int len, char **words, int *word_lens, int max_size,\n\t\t\t\t\t int even_spaces, int remain_spaces, int start, int end)\n{\n\tint i, j;\n\tchar *p = line;\n\tfor (i = start; i < end; i++)\n\t{\n\t\tmemcpy(p, words[i], word_lens[i]);\n\t\tp += word_lens[i];\n\t\tif (i < end - 1)\n\t\t{\n\t\t\tfor (j = 0; j < even_spaces; j++)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t\tif (remain_spaces > 0)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t\tremain_spaces--;\n\t\t\t}\n\t\t}\n\t}\n\twhile (p - line < max_size)\n\t{\n\t\t*p++ = ' ';\n\t}\n\t*p++ = '\\0';\n}\nstatic char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize)\n{\n\tint i, j, k, cap = 100, count = 0;\n\tchar **lines = malloc(cap * sizeof(char *));\n\tchar *buf = malloc(cap * (maxWidth + 1));\n\tfor (i = 0; i < cap; i++)\n\t{\n\t\tlines[i] = buf + i * (maxWidth + 1);\n\t}\n\tint *word_lens = malloc(wordsSize * sizeof(int));\n\tfor (i = 0; i < wordsSize; i++)\n\t{\n\t\tword_lens[i] = strlen(words[i]);\n\t}\n\tint wc = 0;\n\tint len = 0;\n\tint start = 0;\n\tint chars = 0;\n\tfor (i = 0, j = 0; i < wordsSize; i++)\n\t{\n\t\tif (len + word_lens[i] > maxWidth)\n\t\t{\n\t\t\tint even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);\n\t\t\tint remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);\n\t\t\tcount++;\n\t\t\twc = 1;\n\t\t\tlen = word_lens[i] + 1;\n\t\t\tchars = word_lens[i];\n\t\t\tstart = i;\n\t\t}\n\t\telse if (len + word_lens[i] == maxWidth)\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tint even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;\n\t\t\tint remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);\n\t\t\tcount++;\n\t\t\twc = 0;\n\t\t\tlen = 0;\n\t\t\tchars = 0;\n\t\t\tstart = i + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tlen += word_lens[i] + 1;\n\t\t\twc++;\n\t\t}\n\t}\n\tif (wc > 0)\n\t{\n\t\tchar *p = lines[count];\n\t\tfor (i = start; i < start + wc; i++)\n\t\t{\n\t\t\tmemcpy(p, words[i], word_lens[i]);\n\t\t\tp += word_lens[i];\n\t\t\tif (i < start + wc - 1)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t}\n\t\twhile (p - lines[count] < maxWidth)\n\t\t{\n\t\t\t*p++ = ' ';\n\t\t}\n\t\t*p++ = '\\0';\n\t\tcount++;\n\t}\n\t*returnSize = count;\n\treturn lines;\n}\nint main(int argc, char **argv)\n{\n\tif (argc <= 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test maxsize words...\\n\");\n\t\texit(-1);\n\t}\n\tint i, count;\n\tchar **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%s\\n\", lines[i]);\n\t}\n\treturn 0;\n}", "java": "class Solution {\n\tpublic List fullJustify(String[] words, int maxWidth) {\n\t\tList ret = new ArrayList<>();\n\t\tint index = 0;\n\t\twhile (index < words.length) {\n\t\t\tint cur = index, len = 0;\n\t\t\twhile (cur < words.length && len + words[cur].length() + cur - index <= maxWidth) {\n\t\t\t\tlen = len + words[cur++].length();\n\t\t\t}\n\t\t\tcur--;\n\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\tif (cur == words.length - 1) {\n\t\t\t\tfor (int i = index; i <= cur; i++) {\n\t\t\t\t\tsb.append(words[i]);\n\t\t\t\t\tif (i < cur) {\n\t\t\t\t\t\tsb.append(' ');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tint base = cur > index ? (maxWidth - len) / (cur - index) : (maxWidth - len);\n\t\t\t\tString baseStr = genSpace(base);\n\t\t\t\tint left = cur > index ? (maxWidth - len) % (cur - index) : 0;\n\t\t\t\tString leftStr = genSpace(base + 1);\n\t\t\t\tfor (int i = index; i <= cur; i++) {\n\t\t\t\t\tsb.append(words[i]);\n\t\t\t\t\tif (i < cur) {\n\t\t\t\t\t\tsb.append(left > 0 ? leftStr : baseStr);\n\t\t\t\t\t\tleft--;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (sb.length() < maxWidth) {\n\t\t\t\tsb.append(genSpace(maxWidth - sb.length()));\n\t\t\t}\n\t\t\tret.add(sb.toString());\n\t\t\tindex = cur + 1;\n\t\t}\n\t\treturn ret;\n\t}\n\tprivate String genSpace(int n) {\n\t\tchar[] cs = new char[n];\n\t\tArrays.fill(cs, ' ');\n\t\treturn new String(cs);\n\t}\n}", "js": "", "python": "class Solution(object):\n\tdef fullJustify(self, words, maxWidth):\n\t\t\"\"\"\n\t\t:type words: List[str]\n\t\t:type maxWidth: int\n\t\t:rtype: List[str]\n\t\t\"\"\"\n\t\tres = []\n\t\tres_list = []\n\t\tcurr = []\n\t\tcount, pos = 0, 0\n\t\twhile pos < len(words):\n\t\t\tword = words[pos]\n\t\t\tif len(word) > maxWidth:\n\t\t\t\tpos += 1\n\t\t\tif len(word) + count + len(curr)<= maxWidth:\n\t\t\t\tcount += len(word)\n\t\t\t\tcurr.append(word)\n\t\t\t\tpos += 1\n\t\t\telse:\n\t\t\t\tres_list.append(curr)\n\t\t\t\tcurr = []\n\t\t\t\tcount = 0\n\t\tif len(curr) > 0:\n\t\t\tres_list.append(curr)\n\t\tfor index, curr in enumerate(res_list):\n\t\t\ttext = ''\n\t\t\tremain = sum([len(t) for t in curr])\n\t\t\tif len(curr) == 1:\n\t\t\t\ttext = curr[0] + ' ' * (maxWidth - remain)\n\t\t\telif index == len(res_list) - 1:\n\t\t\t\ttext = ' '.join(curr)\n\t\t\t\ttext += ' ' * (maxWidth - remain - len(curr) + 1)\n\t\t\telse:\n\t\t\t\tstep = (maxWidth - remain) / (len(curr) - 1 )\n\t\t\t\textra = (maxWidth - remain) % (len(curr) - 1 )\n\t\t\t\tfor index in range(len(curr) - 1):\n\t\t\t\t\ttext += curr[index] + ' ' * int(step)\n\t\t\t\t\tif extra > 0:\n\t\t\t\t\t\ttext += ' '\n\t\t\t\t\t\textra -= 1\n\t\t\t\ttext += curr[-1]\n\t\t\tres.append(text)\n\t\treturn res\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.fullJustify([\"Don't\",\"go\",\"around\",\"saying\",\"the\",\"world\",\"owes\",\"you\",\"a\",\"living;\",\"the\",\"world\",\"owes\",\"you\",\"nothing;\",\"it\",\"was\",\"here\",\"first.\"],30))", "status": 1, "keywords": "字符串,模拟", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://blog.csdn.net/a1439775520/article/details/104341710" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/67/67_java.ipynb?type=file" }, "notebook_enable": 1 }