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;
}
分享到:
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
leetcode-text 92.Reverse Linked List II Runtime: 4 ms, faster than 67.04% of C online submissions for Reverse Linked List II. Memory Usage: 6.9 MB, less than 100.00% of C online submissions for ...
大佬的leetcode刷题笔记(c++版本)
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题
LeetCode 刷题笔记
(C++)LeetCode刷题题解答案
LeetCode 刷题
leetcode全套解答python版本。包括更新到10月份的的leetcode