`
bcyy
  • 浏览: 1827202 次
文章分类
社区版块
存档分类
最新评论

LeetCode Text Justification

 
阅读更多

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;
	}










分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics