【leetcode】 Text Justification

问题:

给定一个字符串数组words,一个整数L,将words中的字符串按行编辑,L表示每行的长度。

要求:

1)每个单词之间至少是有一个空格隔开的。

2)最后一行每个单词间只间隔一个空格, 最后一个单词后不足L长度的用空格填充。

3)除最后一行外,其他行进行填充长度的空格要均分,不能均分的,将余数代表的空格数依次填充在行左。

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.

说明:

1)边界条件要考虑,L为0,直接返回参数words。L太小一个单词都放不下也要考虑。

2)参数words为“”, L不为0, 要填充L个空格返回。(血的教训,切记)

3)这里需要弄空格填充位数的情况要再说明下,比如当前行包含的word为A、B、C、D,L = 11,则填充后的结果为:

A$$$$B$$$$C$$$D,首先四个单词三处空格,平均后每处3个,剩余的2个要依次加在左边,向完全二叉树的定义一样,允许右子树为空,不允许左为空。

实现:

vector<string> fullJustify(vector<string> &words, int L) {

	vector<string> re;
	const int spaces = 1;
	int wordsNum = words.size();
	if(L == 0)
		return words;
	string line;
	int iword = 0;
	while (iword < wordsNum)
	{
		int i = iword;
		int cur_len = 0;
		//append word.each word divided by one space.
		while(i < wordsNum && cur_len + words[i].size() + spaces <= L + 1)
		{
			cur_len += words[i++].size() + spaces;
		}
		//L too small.
		if(cur_len == 0)
			return re;
		//last line of the text.
		if(i == wordsNum){
			line += words[iword];
			for (int k = iword + 1; k < i; ++k)
			{
				line.append(1, ' ');
				line += words[k];
			}
			line.append(L - line.size(), ' ');
		}
		else{
			int wordsLen = cur_len - (i - iword);
			int fillSpaces = L - wordsLen;
			//just one word in this line.
			if(i - iword == 1)
			{
				line += words[iword];
				line.append(fillSpaces, ' ');
			}
			//
			else
			{
				int eqSpaces = fillSpaces / (i - iword - 1);
				int remainSpaces = fillSpaces % (i - iword - 1);
				for (int ifill = 0; ifill < i - iword - 1; ++ifill)
				{
					//eg:abcd, L = 8, eqSpaces = 8/6 = 1, remainder = 2
					// index 0  1  2  3
					//       a  b  c  d ,
					//a__b__c_d
					line += words[iword + ifill];
					line.append(eqSpaces, ' ');
					if(--remainSpaces >= 0)
						line.append(1, ' ');
				}
				//last word in this line.
				line += words[i - 1];
			}
		}
		iword = i;
		re.push_back(line);
		line.clear();
	}
	return re;
}

后记:

这么个问题,用了几个小时,我cc。

【leetcode】 Text Justification,布布扣,bubuko.com

时间: 2024-10-08 20:04:29

【leetcode】 Text Justification的相关文章

【LeetCode】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. P

【leetcode】Text Justification

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 i

【leetcode】Text Justification(hard) ☆

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 ex

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n