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 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- java网络基础知识的简述
TCP/UDP的介绍 TCP协议:面向连接的,字节流无差错地传输协议. UDP协议:一个不可靠的无连接的数据传输协议. 说明:TCP可以想象成电话通讯,双方在通话时必须建立连接,一方没听清,会要求对方 ...
- Sony/索尼 NW-ZX300A ZX300 无损音乐播放器4.4口
https://item.taobao.com/item.htm?spm=a1z0d.7625083.1998302264.6.5c5f4e69ELHOcm&id=557859816402 ( ...
- Node.js读取文件内容
原文链接:http://blog.csdn.net/zk437092645/article/details/9231787 Node.js读取文件内容包括同步和异步两种方式. 1.同步读取,调用的是r ...
- 20165310_Exp2实验三《敏捷开发与XP实践》
20165310 java_exp3 敏捷开发与XP实践 一.编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法 ...
- ZooKeeper与Kafka相关
Kafka集群搭建: https://www.cnblogs.com/likehua/p/3999538.html https://www.cnblogs.com/mikeguan/p/7079013 ...
- spring与spring-data-redis整合redis
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Java-master(github)教材整理
helloworld class HelloWorld { public static void main(String[] args) { System.out.println("hell ...
- 关于使用jquery的Ajax结合java的Servlet后台判定用户名是否存在
关于把AJAX加入到注册登录demo中去 2018年3月10日 19:21:23 第一次来SUBWAY真切地打代码. 这次的西红柿汤还是挺好喝的. index.jsp: <%@ page con ...
- LINK : fatal error LNK1104: 无法打开文件“libboost_serialization-vc90-mt-gd-1_62.lib”
boost安装:https://www.cnblogs.com/sea-stream/p/10205425.html 在vs中添加
- c++ 多继承 public
以下代码会报错 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void si ...