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 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- Web负载均衡学习笔记之四层和七层负载均衡的区别
0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...
- SNMP学习笔记之SNMP树形结构介绍
Basic command of SNMP: GET: The GET operation is a request sent by the manager to the managed device ...
- 05: python中的函数
1.1 python 函数 1.函数作用 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得 ...
- 20145327 《网络对抗》逆向及BOF基础实践
20145327 <网络对抗>逆向及BOF基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任 ...
- 20145221高其_PC平台逆向破解_advanced
20145221高其_PC平台逆向破解_advanced 实践目录 shellcode注入 Return-to-libc 攻击实验 shellcode注入 概述 Shellcode实际是一段代码(也可 ...
- TensorFlow入门(三)多层 CNNs 实现 mnist分类
欢迎转载,但请务必注明原文出处及作者信息. 深入MNIST refer: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mni ...
- 【前端】Vue.js实现实例搜索应用
实例搜索应用 实现效果: 实现代码与注释: <!DOCTYPE html> <html> <head> <title>实例搜索</title> ...
- RedHat6使用CentOS yum源 换yum
yum 简单介绍一下 yum 主要功能是更方便的添加/删除/更新RPM 包,自动解决包的倚赖性问题,便于管理大量系统的更新问题. yum 可以同时配置多个资源库(Repository),简洁的配置文件 ...
- 【附9】elasticsearch-curator + Linux定时任务
官网教程入口:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html 一.下载安装 下载:sud ...
- 【第二章】 第二个spring-boot程序
上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序.这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用. 1. ...