【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 extra spaces ' ' when necessary so that each line has exactlyL 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.
思路:
写了一天,用100多行才搞定。大神直接10+行秒杀我..... 先看大神代码。
关键点:
①循环内一次性循环出该行的单词
②string 可以初始化指定的空格数
③对最后一行 和 其他行的区分 通过 i + k >= words.size() 只有空格数不同
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> res;
for(int i = , k, l; i < words.size(); i += k) {
for(k = l = ; i + k < words.size() and l + words[i+k].size() <= L - k; k++) {
l += words[i+k].size();
}
string tmp = words[i];
for(int j = ; j < k - ; j++) {
if(i + k >= words.size()) tmp += " "; //当前是最后一行 空格1个
else tmp += string((L - l) / (k - ) + (j < (L - l) % (k - )), ' '); //不是最后一行 均匀空格 如果有多余的匀给前面
tmp += words[i+j+];
}
tmp += string(L - tmp.size(), ' '); //一行最后面如果还有地方 一定是最后一行的空格
res.push_back(tmp);
}
return res;
}
我的代码,其实思路都差不多的。这道题没什么技巧。但是写得非常繁琐。
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ans;
int WordsNum = ;
int WordLength = ;
int totalLength = ;
if(L == )
{
ans.push_back("");
return ans;
}
for(int i = ; i < words.size(); i++)
{
totalLength += words[i].size();
if(WordLength + WordsNum + words[i].size() == L) //恰好放下新的词 这一行确定
{
string LineAns;
for(int j = i - WordsNum; j < i; j++)
{
LineAns += words[j] + " ";
}
LineAns += words[i];
ans.push_back(LineAns);
//初始化下一行的数据
WordsNum = ;
WordLength = ;
}
else if(WordLength + WordsNum + words[i].size() > L) //放不下新的词了
{
string LineAns;
string sGap;
if(WordsNum == ) //只有1个词 最右边补空格
{
LineAns += words[i - ];
int n = L - words[i - ].size();
while(n > )
{
LineAns += " ";
n--;
}
ans.push_back(LineAns);
}
else
{
int Gap = L - WordLength;
int Res = Gap % (WordsNum - );
int BaseGap = Gap / (WordsNum - );
while(BaseGap > )
{
sGap += " ";
BaseGap--;
}
for(int j = i - WordsNum; j < i - ; j++)
{
LineAns += words[j] + sGap;
if(Res > )
{
LineAns += " ";
Res--;
}
}
LineAns += words[i - ];
ans.push_back(LineAns);
}
//初始化下一行的数据
WordsNum = ;
WordLength = words[i].size();
}
else
{
WordsNum++;
WordLength += words[i].size();
}
}
//处理最后一行
string LineAns;
string sGap;
if(WordsNum == )
{
if(totalLength == ) //没有内容 输出一行空格
{
int n = L;
while(n > )
{
LineAns += " ";
n--;
}
ans.push_back(LineAns);
}
}
else
{
for(int j = words.size() - WordsNum; j < words.size() - ; j++)
{
LineAns += words[j] + " ";
}
LineAns += words.back();
while(LineAns.size() < L)
{
LineAns += " ";
}
ans.push_back(LineAns);
}
return ans;
}
【leetcode】Text Justification(hard) ☆的更多相关文章
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【LeetCode】未分类(tag里面没有)(共题)
[334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- 【LeetCode】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
随机推荐
- spring配置文件详解--真的蛮详细
spring配置文件详解--真的蛮详细 转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...
- glusterFS分布式文件系统的搭建
准备工作 1.安装IBA yum install libradmacm librdmacm-devel libmlx4 infiniband-diags 2.配置IPOIB /etc/sysconfi ...
- AD域服务器|两台DC无法进行复制同步
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...
- 浏览器兼容性之Css篇
本文与上一篇随笔<浏览器兼容性之Javascript篇>有一定关联,下来我会继续不断总结,旨在解决浏览器兼容性,对遇到类似问题的同仁有所帮助,如有更多解决浏览器兼容性的案例还望大家分享一起 ...
- IGV软件
它支持各种各样的数据类型,包括基于芯片测序.二代测序数据和基因组注释数据等.整合基因组浏览器(IGV,Integrative Genomics Viewer)进行可视化浏览,它支持各种各式的数据类型, ...
- 在PHP中$_SESSION的使用方法
使用PHP应用session时,将session中的数据存储在服务器上,然后通过客户端传来的sessionID识别客户端的信息,并提取信息. php中的session的常用操作:session的写入. ...
- 解决访问ajax.googleapis.com链接失败方法
参考文章:http://www.jianshu.com/p/690e28f7fde6 主要思路:修改hosts文件,将其网址ajax.googleapis.com指向本地服务器:本地服务器将通过aja ...
- springMVC-1
1.springMVC请求由前端到后端的流程 2.配置过程 (1)需要的jar包 spring-aop.jar spring-beans.jar spring-context.jar spring-c ...
- php综合应用
php面试题之五--PHP综合应用(高级部分) 五.PHP综合应用 1.写出下列服务的用途和默认端口(新浪网技术部) ftp.ssh.http.telnet.https ftp:File Transf ...
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events. ...