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. 前端性能利器——dynatrace ajax edition

    因为最近的工作跟性能分析有关系,所以写个小总结. 顺带推荐两个我常用的小工具: 1.文件对比工具beyond compare,非常好用,对比.修改很简单.当然我只是用的试用版本.google一下官网下 ...

  2. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)

    urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的一些 ...

  3. Linux 中常见的命令行,持续更新

    1.添加自己的环境变量 root@adonis:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin r ...

  4. 一张图告诉你,只会jQuery还不够!

    会了jquery语法,会了jquery函数,你就真的会了jquery吗,来看这张图!是超实用的jquery代码段一书的导览!熊孩子们,赶紧学习去吧! 对于码农来说,代码就是生产力,你每天能码多少行并不 ...

  5. jdk版本及编译版本导致服务器部署UnsupportedClassVersionError错误

    java本地代码运行正常,部署到服务器无法运行,错误如下: Caused by: java.lang.UnsupportedClassVersionError: com/teshehui/cms/ac ...

  6. 使一个div始终显示在页面中间

    使一个div始终显示在页面中间 假设我们有一个div层:<div id=”myDiv”></div> 首先,我们用css来控制它在水平上始终居中,那么我们的css代码应该是这样 ...

  7. tcp三次握手与四次挥手

  8. 淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接

    首先,我建立了一个包,里面存放了三个类文件,这三个文件是我从网络中找的,经过自己的整理.(我刚才查找想把这三个文件传上去,可能是自己对cnblogs的博客不太熟悉吧,没有找到,我只好粘贴代码了) 三个 ...

  9. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  10. iOS开发——网络篇——NSURLSession,下载、上传代理方法,利用NSURLSession断点下载,AFN基本使用,网络检测,NSURLConnection补充

    一.NSURLConnection补充 前面提到的NSURLConnection有些知识点需要补充 NSURLConnectionDataDelegate的代理方法有一下几个 - (void)conn ...