68. 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 extra spaces ' ' when necessary so that each line has exactly L characters.
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 exceed L in length.
- 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.
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n = words.size(), l, extra, eachex, i, j, k, t;
vector<int> len(n, );
for(i=; i<n; i++)
len[i] = words[i].length();
vector<string> ans;
i=;
while(i<n)
{
for(l=len[i], j=i+; j<n && l<=maxWidth; j++)
l += len[j]+;
string s = words[i];
if(j==n && l<=maxWidth) //the last line
{
for(k = i+; k<j; k++)
{
s += ' ';
s += words[k];
}
}
else
{
j--;
l -= len[j]+;
extra = maxWidth-l;
eachex = (j>i+) ? extra / (j-i-) : extra;
for(k=i+; k<j; k++)
{
if(extra)
{
if((j == i) || (extra == eachex*(j-k)))
{
for(t=; t<eachex; t++)
s += ' ';
extra -= eachex;
}
else
{
for(t=; t<eachex+; t++)
s += ' ';
extra -= eachex+;
}
}
s += ' ';
s += words[k];
}
}
for(k=s.length(); k<maxWidth; k++)
s += ' ';
ans.push_back(s);
i = j;
}
return ans;
}
};
测试用例:
["a","b","c","d","e"] 1 -- ["a","b","c","d","e"]
["Here is an","example of text","justification. "]
["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
30 --
["Don't go around saying the","world owes you a living; the","world owes you nothing; it was","here first. "]
68. Text Justification *HARD*的更多相关文章
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 68. Text Justification (JAVA)
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- tensorflow mnist 给一张手写字辨别
https://www.jianshu.com/p/db2afc0b0334 https://blog.csdn.net/xxzhangx/article/details/54563574
- 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)
brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...
- 网络压缩论文集(network compression)
Convolutional Neural Networks ImageNet Models Architecture Design Activation Functions Visualization ...
- Ubuntu16.04 无法连接WiFi
在安装完 ns-3.25 之后,着手开始准备 Eclipse 的安装,打开了 Firefox游览器 准备上网的时候,发现网络没有正常连接. 刚刚开始怀疑的是,并没有连接上网络. 于是打开了终端,pin ...
- 非[无]root权限 服务器 下安装perl以及perl模块--转载
转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...
- python PIL 图像处理库简介(一)
1. Introduction PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处 ...
- 利用JSP中的过滤器解决中文乱码问题
首先我们创建过过滤器: package com.gbx; import java.io.IOException; import javax.servlet.Filter; import javax.s ...
- Intel微处理器学习笔记(三) 不可见寄存器
参考资料: 1. http://blog.chinaunix.net/uid-20797642-id-2495244.html 2. http://www.techbulo.com/708.htm ...
- 转载:负载均衡器技术Nginx和F5的优缺点对比
https://blog.csdn.net/zxc456733/article/details/78861100 nginx(一) nginx详解 nginx是一个被广泛使用的集群架构组件,我们有必要 ...
- 【Golang 接口自动化03】 解析接口返回XML
上一篇我们学习了怎么发送各种数据类型的http请求,这一篇我们来介绍怎么来解析接口返回的XML的数据. 解析接口返回数据 定义结构体 假设我们现在有一个接口返回的数据resp如下: <?xml ...