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) ☆的更多相关文章

  1. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  4. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  5. 【LeetCode】未分类(tag里面没有)(共题)

    [334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...

  6. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  7. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  8. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  9. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

随机推荐

  1. pipe-filter 真难找啊

    http://blog.csdn.net/absurd/article/details/4307903

  2. 游戏BUFF设计

    游戏中的BUFF/DEBUFF我们见过很多,我见到的玩得比较泛滥的就属WAR3.魔兽世界.九阴真经.仿DOTA类的如LOL. 总体上来说,BUFF/DEBUFF都属于“临时的技能效果”,因此它们可以沿 ...

  3. 【C语言入门教程】4.8 指针数组

    指针数组是一种特殊的数组,这类数组存放的全部是同一数据类型的内存地址.指针数组的定义形式为: 数据类型 *数组名[长度]; 例如: const char *c[4] = { "China&q ...

  4. android 弹出的软键盘遮挡住EditText文本框的解决方案

    1.android 弹出的软键盘遮挡住EditText文本框的解决方案: 把Activit对应的布局文件filename.xml文件里的控件用比重设置布局.(例如:android:layout_wei ...

  5. PHP中函数sprintf .vsprintf (占位符)

    sprintf()格式化字符串写入一个变量中. vsprintf()格式化字符串些写入变量中. <?php $num1 = 123; $num2 = 456; $txt = vsprintf(& ...

  6. PictureCutting图片批量裁切(裁剪)工具

    PictureCutting图片批量裁切(裁剪)工具 写这个工具的原因是因为在获取一个软件的皮肤包中的图片的时候需要进行批量的裁切工作,而有没有找到在linux下简单好用的工具,干脆就用QT写了一个. ...

  7. fastx_toolkit软件使用说明

    高通量测序数据下机后的原始fastq文件,包含4行,其中一行为质量值,另外一行则为对应序列,我们都了解高通量的数据处理首先要进行质量控制,这些过程包括去接头.过滤低质量reads.去除低质量的3'和5 ...

  8. 一次Android脱壳training

    一.查壳 jeb载入发现没有代码,怀疑加壳 用查壳工具查壳 (爱加密) apktool解包 得到其 package name: loading.androidmanual main activity ...

  9. Visual Studio 2010添加新项缺失[ADO.NET 实体数据模型]解决方法

    当进行ASP.NET MVC项目开发,准备使用EF进行数据库访问,我的开发模式是"Table First".于是,准备在Model目录新建EF的数据表映射文件.可是,在添加新项目窗 ...

  10. SCP 和 rsync限速以及用法

    rsync限速以及用法 -- :: 标签:限速 rsync 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xficc.blog. ...