Given an array of words and a length L, format the text such that each line has exactly L 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 Lcharacters.
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.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. " ]
Note: Each word is guaranteed not to exceed L in length.
- A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
justify(vector<string> word, int L, bool flag = false)
这个函数可以对最后一行或者非最后一行的单词进行处理,直接返回处理完的结果。
主要的几个变量是spaces:记录需要填充的空格总数,slots:记录一行单词的空隙数,remain:单词之间需要额外填充的空格数。
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Note: The Solution object is instantiated only once and is reused by each test case.
// result returns the final result, wordArray is used to deal with string that length is
// less than L
vector<string> result, wordArray;
if (L == 0 || words.empty()) return result;
int num = words.size();
// the loop can just deal with num-1 lines
// because in each loop we should decide whether to put the i'th word into wordArray
for (int i = 0, len = 0; i < num; ++i)
{
if (wordArray.size() == 0)
{
len = words[i].length();
wordArray.push_back(words[i]);
}
else if (len + words[i].length() < L)
{
len = len + words[i].length() + 1;
wordArray.push_back(words[i]);
}
else
{
--i;
result.push_back(justify(wordArray, L, false));
wordArray.clear();
}
}
//deal with last line
result.push_back(justify(wordArray, L, true));
return result;
}
private:
string justify(vector<string> word, int L, bool flag = false)
{
//if flag = false that means we are not dealing with last line
//avg means average spaces between words
//remain means how many extra spaces remaining to deal
int num = word.size(), slots = num - 1;
int avg = 0, spaces = 0, numOfChar = 0, remain = 0;
string ret;
//count how many spaces are needed
for (int i = 0; i < num; ++i)
{
numOfChar += word[i].length();
}
spaces = L - numOfChar;
if (slots == 0)
{
remain = spaces;
avg = 0;
}
else
{
remain = spaces % slots;
avg = spaces / slots;
}
if (flag)
{
avg = 1;
remain = 0;
}
//insert word and space into string
for (int i = 0, avgSpace; i < num; ++i)
{
avgSpace = avg;
ret.append(word[i]);
//insert space to every slot
while (avgSpace && i < num - 1)
{
ret.append(" ");
--avgSpace;
}
if (remain)
{
ret.append(" ");
--remain;
}
}
//if flag is true,then next line will run
while (ret.length() < L) ret.append(" ");
return ret;
}
};