Description
Given an array of words and a length l
, format the text such that each line has exactly l
characters and is fully justified on both the left and the right. Words should be packed in a greedy approach; that is, pack as many words as possible in each line. Add extra spaces when necessary so that each line has exactly l
characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text and lines with one word only, the words should be left justified with no extra space inserted between them.
Example
For words = ["This", "is", "an", "example", "of", "text", "justification."]
and l = 16
, the output should be
textJustification(words, l) = ["This is an",
"example of text",
"justification. "]
Input/Output
-
[execution time limit] 0.5 seconds (cpp)
-
[input] array.string words
- An array of words. Each word is guaranteed not to exceed
l
in length. - Guaranteed constraints:
1 ≤ words.length ≤ 150
,0 ≤ words[i].length ≤ l
.
- An array of words. Each word is guaranteed not to exceed
-
[input] integer l
- The length that all the lines in the output array should be.
- Guaranteed constraints:
1 ≤ l ≤ 60
.
-
[output] array.string
- The formatted text as an array containing lines of text, with each line having a length of
l
.
- The formatted text as an array containing lines of text, with each line having a length of
solution
大致思想是首先计算一行能够容纳多少个单词,在计算容纳单词数时需要加上单词与单词之间的空格。然后如果一行中单词加上单词之间的一个空格恰好等于 l
,则直接将临时字符串存入结果数组。如果小于,则需要在单词之间补充额外的空格,具体数量如下:
- 首先用一行的长度
l
减去该行的单词数量得到总的需要添加的空格数,记为space
; - 假设单词数量为 n, 则需要在单词之间补充 n - 1 组空格(每一组的空格数可能不等)
- 那么每一组都需要补充
space / (n - 1)
个空格 - 剩余的前
space % (n - 1)
处还需要再补充一个空格
#include <iostream>
#include <vector>
std::vector<std::string> textJustification(std::vector<std::string>& words, int l) {
std::vector<std::string> res;
for (int i = 0; i < words.size();) {
int start = i;
std::string temp = words[i];
i++;
while(i < words.size() && (temp + " " + words[i]).length() <= l) {
temp += " " + words[i];
i++;
}
if (temp.length() == l) { // 如果长度刚好,则不需要扩充空格
res.emplace_back(temp);
} else {
if (i - start == 1 || i >= words.size()) { // 如果只有一个单词或者单词已用完
while (temp.length() < l) temp += " "; // 需要补充空格
res.emplace_back(temp);
} else {
int spaces = l - temp.length() + i - start - 1; // 一行中需要补充的空格数量
int perWord = spaces / (i - start - 1); // 每个原始有空格的地方需要添加的空格数
int extras = spaces % (i - start - 1); // 还需要补充空格的前 extras 处
temp = "";
for (int j = start; j < i-1; j++) {
temp += words[j];
for (int k = 0; k < perWord; ++k) {
temp += " ";
}
if (j - start < extras) temp += " ";
}
temp += words[i-1];
res.emplace_back(temp);
}
}
}
return res;
}
template<typename T>
void print1DVector(const std::string& str, std::vector<T>& array) {
std::cout << str << ": ";
for (auto arr: array) {
std::cout << arr << '\n';
}
}
int main() {
std::vector<std::string> words{"This", "is", "an", "example", "of", "text", "justification."};
std::vector<std::string> res = textJustification(words, 16);
print1DVector("Justifi text", res);
return 0;
}