[LeetCode] Text Justification

本文介绍了一种文本格式化算法,该算法能够确保每行文本恰好达到指定长度,并实现左右两端对齐的效果。通过贪婪填充尽可能多的单词,并在必要时均匀分布额外的空格,以确保每行达到规定的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Problem]

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 L 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.

Corner Cases:
  • 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.

[Solution]

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

vector<string> res;
int len = 0, start = 0, end = 0;
while(end < words.size()){
// try to add a word
if(start == end){
len = words[end].length();
}
else{
len = len + 1+ words[end].length();
}

// full
if(len == L){
// generate line
string line = words[start];
for(int i = start+1; i <= end; ++i){
line += " " + words[i];
}
res.push_back(line);

// update end
end++;
start = end;
}
// over flow
else if(len > L){
if(end - start == 1){
string line = words[start] + string(L - words[start].length(), ' ');
res.push_back(line);
}
else{
// get words length
len = 0;
for(int i = start; i <= end-1; ++i){
len += words[i].length();
}

// number of spaces in this line
int space = L - len;

// number of spaces between two words
int average = space / (end - start - 1);
int more = space % (end - start - 1);

// generate line
string line = words[start];
for(int i = start+1; i <= end-1; ++i){
if(i - start <= more){
line += string(average+1, ' ') + words[i];
}
else{
line += string(average, ' ') + words[i];
}
}
res.push_back(line);
}
start = end;
}
else{
end++;
}
}

// be careful to add the last line
if(end > 0 && start < words.size()){
string line = words[start];
for(int i = start+1; i < end; ++i){
line += " " + words[i];
}

// fill spaces
line += string(L - line.length(), ' ');
res.push_back(line);
}

return res;
}
};


 说明:版权所有,转载请注明出处。 Coder007的博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值