难度:Hard
原题连接
内容描述
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 "
]
思路 - 时间复杂度: O(n)- 空间复杂度: O(n)******
题目不难,根据题目的意思先计算出一行最多能容纳多少单词,每个单词之间以一个空格分割就可以计算出每行最多能容纳多少单词。接下来计算每行的正确格式。计算出总共的空格数除以间隔数。总空格数除以间隔数,若余数为0,则商为单词之间的空格数,若余数 r 不为0,则从头开始 r 个空格数加1,剩下的不变。最后一行要注意每个单词之间的空格数为1,字符数不满maxWidth,剩下的用空格补充。
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int l = 0,r = 0,sum = 0;
vector<string> ans;
for(int i = 0;i < words.size();++i)
{
sum += words[i].length();
if(sum <= maxWidth)
{
sum += 1;
r++;
}
else
{
string temp;
sum -= (r - l + words[i].length());
int residue = 0, divide = (maxWidth - words[l].length());
if(l + 1 != r)
{
residue = (maxWidth - sum) % (r - l - 1);
divide = (maxWidth - sum - residue) / (r - l - 1);
}
temp.append(words[l].begin(),words[l].end());
for(int j = l + 1;j < r - 1;++j)
{
int k = residue > 0 ? divide + 1 : divide;
while(k)
{
temp.push_back(' ');
--k;
}
temp.append(words[j].begin(),words[j].end());
residue--;
}
int k = residue > 0 ? divide +1 : divide;
while(k)
{
temp.push_back(' ');
--k;
}
if(l + 1 != r)
temp.append(words[r - 1].begin(),words[r - 1].end());
ans.push_back(temp);
l = r++;
sum = words[i].length() + 1;
}
}
sum -= 1;
string temp;
temp.append(words[l].begin(),words[l].end());
for(int i = l + 1;i < r;++i)
{
temp.push_back(' ');
temp.append(words[i].begin(),words[i].end());
}
int k = maxWidth - sum;
while(k)
{
temp.push_back(' ');
--k;
}
ans.push_back(temp);
return ans;
}
};