|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=68 lang=cpp |
| 3 | + * |
| 4 | + * [68] 文本左右对齐 |
| 5 | + * |
| 6 | + * https://leetcode.cn/problems/text-justification/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Hard (52.22%) |
| 10 | + * Likes: 334 |
| 11 | + * Dislikes: 0 |
| 12 | + * Total Accepted: 53.9K |
| 13 | + * Total Submissions: 103.2K |
| 14 | + * Testcase Example: '["This", "is", "an", "example", "of", "text", |
| 15 | + * "justification."]\n16' |
| 16 | + * |
| 17 | + * 给定一个单词数组 words |
| 18 | + * 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。 |
| 19 | + * |
| 20 | + * 你应该使用 "贪心算法" |
| 21 | + * 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' |
| 22 | + * ' 填充,使得每行恰好有 maxWidth 个字符。 |
| 23 | + * |
| 24 | + * 要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。 |
| 25 | + * |
| 26 | + * 文本的最后一行应为左对齐,且单词之间不插入额外的空格。 |
| 27 | + * |
| 28 | + * 注意: |
| 29 | + * |
| 30 | + * |
| 31 | + * 单词是指由非空格字符组成的字符序列。 |
| 32 | + * 每个单词的长度大于 0,小于等于 maxWidth。 |
| 33 | + * 输入单词数组 words 至少包含一个单词。 |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * |
| 38 | + * 示例 1: |
| 39 | + * |
| 40 | + * |
| 41 | + * 输入: words = ["This", "is", "an", "example", "of", "text", |
| 42 | + * "justification."], maxWidth = 16 输出: |
| 43 | + * [ |
| 44 | + * "This is an", |
| 45 | + * "example of text", |
| 46 | + * "justification. " |
| 47 | + * ] |
| 48 | + * |
| 49 | + * |
| 50 | + * 示例 2: |
| 51 | + * |
| 52 | + * |
| 53 | + * 输入:words = ["What","must","be","acknowledgment","shall","be"], maxWidth = |
| 54 | + * 16 输出: |
| 55 | + * [ |
| 56 | + * "What must be", |
| 57 | + * "acknowledgment ", |
| 58 | + * "shall be " |
| 59 | + * ] |
| 60 | + * 解释: 注意最后一行的格式应为 "shall be " 而不是 "shall be", |
| 61 | + * 因为最后一行应为左对齐,而不是左右两端对齐。 |
| 62 | + * 第二行同样为左对齐,这是因为这行只包含一个单词。 |
| 63 | + * |
| 64 | + * |
| 65 | + * 示例 3: |
| 66 | + * |
| 67 | + * |
| 68 | + * 输入:words = |
| 69 | + * ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"],maxWidth |
| 70 | + * = 20 |
| 71 | + * 输出: |
| 72 | + * [ |
| 73 | + * "Science is what we", |
| 74 | + * "understand well", |
| 75 | + * "enough to explain to", |
| 76 | + * "a computer. Art is", |
| 77 | + * "everything else we", |
| 78 | + * "do " |
| 79 | + * ] |
| 80 | + * |
| 81 | + * |
| 82 | + * |
| 83 | + * |
| 84 | + * 提示: |
| 85 | + * |
| 86 | + * |
| 87 | + * 1 <= words.length <= 300 |
| 88 | + * 1 <= words[i].length <= 20 |
| 89 | + * words[i] 由小写英文字母和符号组成 |
| 90 | + * 1 <= maxWidth <= 100 |
| 91 | + * words[i].length <= maxWidth |
| 92 | + * |
| 93 | + * |
| 94 | + */ |
| 95 | + |
| 96 | +#include <string> |
| 97 | +#include <vector> |
| 98 | +using namespace std; |
| 99 | + |
| 100 | +// @lc code=start |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + vector<string> fullJustify(vector<string> &words, int maxWidth) { |
| 104 | + vector<string> ret; |
| 105 | + if (words.empty()) { |
| 106 | + return ret; |
| 107 | + } |
| 108 | + int l = 0, r = 0, n = words.size(); |
| 109 | + while (true) { |
| 110 | + int lineWidth = 0; |
| 111 | + while (r < n && lineWidth + r - l + words[r].length() <= maxWidth) { |
| 112 | + lineWidth += words[r++].length(); |
| 113 | + } |
| 114 | + // last line |
| 115 | + if (r == n) { |
| 116 | + string s = join(words, l, r, " "); |
| 117 | + ret.emplace_back(s + blank(maxWidth - s.length())); |
| 118 | + break; |
| 119 | + } |
| 120 | + // one word |
| 121 | + if (r - l == 1) { |
| 122 | + ret.emplace_back(words[l] + blank(maxWidth - words[l].length())); |
| 123 | + l++; |
| 124 | + continue; |
| 125 | + } |
| 126 | + // more than one word |
| 127 | + int wordNum = r - l; |
| 128 | + int spaceNum = maxWidth - lineWidth; |
| 129 | + int avgSpace = spaceNum / (wordNum - 1); // segregation num: wordNum - 1 |
| 130 | + int extraSpace = spaceNum % (wordNum - 1); |
| 131 | + string s1 = join(words, l, l + extraSpace + 1, blank(avgSpace + 1)); |
| 132 | + string s2 = join(words, l + extraSpace + 1, r, blank(avgSpace)); |
| 133 | + ret.emplace_back(s1 + blank(avgSpace) + s2); |
| 134 | + l = r; |
| 135 | + } |
| 136 | + |
| 137 | + return ret; |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + string blank(int n) { return string(n, ' '); } |
| 142 | + string join(vector<string> &words, int l, int r, string seg) { |
| 143 | + string ret(words[l]); |
| 144 | + for (size_t i = l + 1; i < r; i++) { |
| 145 | + ret += seg + words[i]; |
| 146 | + } |
| 147 | + return ret; |
| 148 | + } |
| 149 | +}; |
| 150 | +// @lc code=end |
0 commit comments