Text Justification(文本对齐)

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."]
L16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in 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.
这道题目本身没有复杂的算法,不过要做对确实不容易(提交了5次才AC~(>_<)~),要理解清楚题意,题意最后一点是说 如果处理到最后一行数据,单词之间只间隔1个空格,剩余的用空格填充(这点很重要)。 这里我的算法是先设计一个函数
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;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值