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. grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)

    这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解 ...

  2. ASP.NET 大文件下载的实现思路及代码

    文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代 ...

  3. [译]git rebase -i

    使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...

  4. 由pthread_create引起的段错误

    一般线程的结束是由进程内的其他线程来结束的,调用pthread_cancel. 但是需要考虑到被结束线程的性质,一方面,线程是可被结束,也可无法结束,即不响应该信号:另一方面,如果线程是可被结束的,那 ...

  5. NHibernate配置

    因为NHibernate被设计为可以在许多不同环境下工作,所以它有很多配置参数.幸运的是,大部分都已经有默认值了. NHibernate.Test.dll包含了一个示例的配置文件app.config, ...

  6. IDEA之创建不了.java文件解决

    1.问题:在IDEA中新建的maven项目,无法创建.java文件 从上图看出,在new对应的栏目中没有java class选项 2.解决 这是因为maven的配置问题 应该如下: 注:如果这样还不行 ...

  7. js引出函数概念的案例

    js引出函数概念的案例   1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8&q ...

  8. CentOS6系升级Python2.7版本

    安装前准备 本实例以CentOS6.7为例 [root@E tools]# uname -r 2.6.32-431.23.3.el6.x86_64 [root@E tools]# uname -m x ...

  9. Oracle 10G select工作原理

    数据库查询语句内部执行过程 select * from  table 步骤 分析阶段(parse) 1.共享池库高速缓存有没有该语句.如果有直接返回结果. 2.语法分析sql语句是否正确进行下一步分析 ...

  10. 跟着百度学PHP[4]-OOP面对对象编程-3-实例化一个对象

    当定义好类后,我们使用new关键字来实例化一个对象! 格式: $object = new 类名; <?php class Person{ private $name; "; priva ...