LeetCode Text Justification

本文介绍了一种文本两端对齐的算法实现,该算法能够处理特定长度限制下的文本格式化问题,确保每行文本都能恰好填满指定长度,并且均匀分布单词间的空格。文章详细解释了算法的实现细节,包括特殊情况下如何处理单个单词占据一行以及最后一行的对齐方式。

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

Text Justification


Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters 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 exactlyLcharacters.

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 exceedLin length.

click to show corner cases.

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.

这道题属于思路不难,但是很繁的题目,程序十分长,需要十分细心,很考编程功底的题目吧。

分了两个特殊情况需要额外处理的:

1 只有一个单词能容纳到一行的时候

2 结尾,最后一行的时候

都需要作为左靠齐处理。还好,功力不错了,处理完两个特殊情况之后,马上就AC了。

思路:

1 计算一行可以容纳多少个单词

2 计算这行单词间需要插入多少个空格,单词间最少插入一个空格

3 处理上面两个特殊情况

翻译为程序,OK。

class Solution {
public:
	vector<string> fullJustify(vector<string> &words, int L) {
		vector<string> ans;
		if (words.empty()) return ans;

		size_t ansI = -1;
		for (size_t i = 0; i < words.size();)
		{
			size_t c = 0;
			size_t len = 0;

			//必须有空格分开,所以要增加额外空格判断-题目特殊要求(需要问清题目要求)
			int space = 0;
			while (i+c < words.size() && 
					len+words[i+c].length()+space <= (size_t)L)
			{
				len += words[i+c].length();
				c++;
				space++;
			}
			if (c == 0) c++;
			ans.push_back(words[i]);
			ansI++;
			space = L - len;
			/*
			原来还有特别要求:
Input:	["What","must","be","shall","be."], 12
Output:	["What must be","shall    be."]
Expected:	["What must be","shall be.   "]
			*/
			if(i+c == words.size())
			{
				size_t r = 0;
				while (r < c-1)
				{
					ans[ansI] += ' ';
					r++;
					ans[ansI] += words[i+r];
				}
				for (size_t j = 0; j < space-r; j++)
				{
					ans[ansI] += ' ';
				}
				break;
			}
			//有一个单词后面必须有空格,所以要额外处理(需要问怎么处理)
			if (c == 1)
			{
				for (int j = 0; j < space; j++)
				{
					ans[ansI] += ' ';
				}
			}
			else
			{
				size_t j = 0, k = 0;
				j = space/(c-1);
				k = space%(c-1);
				size_t r = 0;
				while (r < c-1)
				{
					for (size_t index = 0; index < j; index++)
					{
						ans[ansI] += ' ';
					}
					if (k > 0)  
					{
						ans[ansI] += ' ';
						k--;
					}
					r++;
					ans[ansI] += words[i+r];
				}
			}
			i+=c;
		}
		return ans;
	}
};


//2014-2-9 update
	vector<string> fullJustify(vector<string> &words, int L) 
	{
		vector<string> rs;
		
		for (int i = 0; i < words.size(); )
		{
			int j = i+1;
			int len = words[i].length();
			for (; j < words.size() && len+words[j].length()<L; j++)
				len += 1 + words[j].length();
			
			if (j == words.size())
			{
				string s(words[i]);
				for (i++ ; i < j; i++) s +=" "+words[i];
				while (s.length() < L) s.push_back(' ');
				rs.push_back(s);
				return rs;
			}
			if (j-i == 1) 
			{
				rs.push_back(words[i++]);
				rs.back().append(L-rs.back().length(), ' ');
				continue;
			}

			int a = (L-len) / (j-i-1) + 1;
			int b = (L-len) % (j-i-1);
			string s(words[i]);
			for (i++; i < j; i++, b--)
			{
				s.append(a,' ');
				if (b>0) s.push_back(' ');
				s.append(words[i]);
			}
			rs.push_back(s);
		}
		return rs;
	}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值