题目描述:
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly maxWidth characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do 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, it should be left justified and no extra space is inserted between words.
Note:
A word is defined as a character sequence consisting of non-space characters only.
Each word’s length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.
Example 1:
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is “shall be ” instead of “shall be”,
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.
Example 3:
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
题源:here;完整实现:here
思路:
题干这么长是不是被吓到了。其实这道题你直接看给的例子就知道了:要实现文本的两端对齐,最后一行左对齐。这种类型的题需要的是细致的思考,思考方式依然是由粗到细。代码如下:
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
int left = 0, right = 0, wordsLen = words.size();
int lenCurr = 0, charLen = 0;
while (right < wordsLen){
lenCurr += words[right].size();
if (lenCurr == maxWidth){
string temp;
for (int i = left; i < right; i++) temp += words[i] + " ";
temp += words[right]; result.push_back(temp);
right++; left = right; lenCurr = 0; charLen = 0;
}
else if (lenCurr > maxWidth){
int wordsNum = right - left;
if (wordsNum == 1){
string temp = words[left];
while (temp.size() < maxWidth) temp += " ";
result.push_back(temp);
}
else{
int spaceNum = maxWidth - charLen;
int spaceEach = spaceNum / (wordsNum - 1);
int remainder = spaceNum % (wordsNum - 1);
string temp;
for (int i = left; i < right-1; i++){
temp += words[i];
for (int j = 0; j < spaceEach; j++) temp += " ";
if (i - left < remainder) temp += " ";
}
temp += words[right - 1];
result.push_back(temp);
}
left = right; lenCurr = 0; charLen = 0;
}
else {
charLen += words[right].size();
lenCurr++; right++;
}
}
if (left < right){
string temp;
for (int i = left; i < right; i++){
temp += words[i] + " ";
}
while (temp.size() < maxWidth) temp += " ";
result.push_back(temp);
}
return result;
}
};
最后贴出运行结果纪念一下:

本文介绍了一种文本两端对齐的算法实现,该算法能够确保每行文本在规定的宽度内完全对齐,同时考虑到了最后一行左对齐的需求。通过具体实例展示了如何处理不同长度的单词和行宽。

349

被折叠的 条评论
为什么被折叠?



