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 exactlyL 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.
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.
Subscribe to see which companies asked this question
思路分析:
贪心算法的思想。
/*
*/
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Solution_068_TextJustification
{
private:
vector<string> ret;
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
int i = 0;
while (i < words.size())
{
int size = 0;
int beg = i;
while (i < words.size())
{
int newSize = size == 0 ? words[i].size() : size + 1 + words[i].size();
if (newSize <= L)
size = newSize;
else
break;
i++;
}
int spaceCount = L - size;
int everyCount;
if (i - 1 - beg != 0 && i != words.size())
{
everyCount = spaceCount / (i - 1 - beg);
spaceCount %= (i - 1 - beg);
}
else
everyCount = 0;
string s;
for (int j = beg; j < i; j++)
{
if (j == beg)
{
s = words[j];
}
else
{
s += ' ';
for (int k = 0; k < everyCount; k++)
s += ' ';
if (spaceCount > 0 && i != words.size())
{
s += ' ';
spaceCount--;
}
s += words[j];
}
}
for (int j = 0; j < spaceCount; j++)
s += ' ';
ret.push_back(s);
}
return ret;
}
};
本文介绍了一种使用贪心算法实现的文本格式化方法,确保每行文本恰好达到指定长度,并实现左右对齐效果。文章详细解释了如何均匀分配空格、处理最后一行的特殊情况,以及解决当一行中只有一个单词时的对齐问题。
338

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



