【LeetCode】68. Text Justification
Text Justification
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.
- 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.
贪心算法,主要这几步骤:
使用字符串line记录当前行
1、计算能否加入下一个单词word
2、若长度L能容纳,则加入line,并更新line长度
3、若长度L不能容纳,则line即为符合条件的一行,并重新调整单词间隔,构成newline
4、单词全部添加完毕,在newline尾部加入空格。
注意:添加最后一行
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ret;
if(words.empty())
return ret;
string line = words[];
int len = words[].size();
int begin = ;
int end = ;
for(int i = ; i < words.size(); i ++)
{
len = len + + words[i].size();
if(len <= L)
{//can insert words[i]
line = line + " " + words[i];
}
else
{
len = len - - words[i].size();
end = i-;
int extraspaces = L - len;
string newline;
for(int j = begin; j < end; j ++)
{
string slot(ceil((double)extraspaces/(end-j)), ' ');
newline = newline + words[j] + " " + slot;
extraspaces -= slot.size();
}
newline += words[end];
string extraStr(L-newline.size(), ' ');
ret.push_back(newline+extraStr);
line = words[i];
len = words[i].size();
begin = i;
}
}
string extraStr(L-line.size(), ' ');
ret.push_back(line+extraStr);
return ret;
}
};

【LeetCode】68. Text Justification的更多相关文章
- 【LeetCode】 -- 68.Text Justification
题目大意:给定一个数组容器,里面存有很多string: 一个int maxWith.让你均匀安排每一行的字符串,能够尽可能的均匀. 解题思路:字符串+贪心.一开始想复杂了,总觉的题意描述的不是很清楚, ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LEETCODE】68、动态规划,medium级别,题目:95、120、91
package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y201 ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 论文阅读(Xiang Bai——【arXiv2016】Scene Text Detection via Holistic, Multi-Channel Prediction)
Xiang Bai--[arXiv2016]Scene Text Detection via Holistic, Multi-Channel Prediction 目录 作者和相关链接 方法概括 创新 ...
- 论文阅读(Xiang Bai——【CVPR2015】Symmetry-Based Text Line Detection in Natural Scenes)
Xiang Bai--[CVPR2015]Symmetry-Based Text Line Detection in Natural Scenes 目录 作者和相关链接 方法概括 创新点和贡献 方法细 ...
- 论文阅读(Xiang Bai——【CVPR2016】Multi-Oriented Text Detection with Fully Convolutional Networks)
Xiang Bai--[CVPR2016]Multi-Oriented Text Detection with Fully Convolutional Networks 目录 作者和相关链接 方法概括 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- AJAX传递数组
在前台中Jq代码中中用JSON.stringify()将数组转换成 JSON字符串.在后台用json_decode()将JSON字符串转换成数组. 1.JSON.stringify(value [, ...
- unix/linux中如何在vi编辑器中方便的跳转到首行和末行?
1.跳转到首行,:0/:1 2.跳转到末行,shift +g/G/:$
- 解决新版本webpack vue-cli生成文件没有dev.server.js问题
新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...
- Linux下swap分区多大才合适的问题探讨
说明: 1.这个话题在每个Linux发行版中都各不相同,且在当下内存硬盘的时代下,再组个磁盘阵列之后速度相当. 2.我觉得硬盘大的情况下,有多大搞多大,我只要控制内存的使用率在99%时才使用swap, ...
- 让ios项目同时支持ARC和非ARC
ttp://code4app.com/snippets/one/禁止某几个文件用ARC模式编译/502344256803fa246d000000#s0 如果你的绝大部分代码需要ARC,那么就设置项目支 ...
- Centos7 内核从3.10升级到4.12过程
http://blog.csdn.net/youshijifen/article/details/73472434
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
由于项目需要,需要用NGUi实现一个图片浏览切换的功能,于是参考官方NGUI例子的ScrollView做了一个例子,初始看上去基本实现了自己想要的功能. 但是测试后发现当隐藏其中一张图片后,后面图片不 ...
- 提交svn的时候,提示丢失了预定增加的xxxx
百度之,发现都是linux或者命令行下的解决方法, 经测试,右键svn目录 选择tortoisesvn 选择svn还原, 即可解决问题
- 仿LOL项目开发第五天
仿LOL项目开发第五天 by草帽 今天呢,我们看下能开发什么内容,首先上节我们已经讲了UI框架的搭建,上节还遗留下很多问题,比如说消息的字符是代码里面自己赋值的. 那么就比较死板,按照正常的逻辑,那些 ...
- EF DBContext中DbSet中Hashset添加对象后,DataGrid UI没有刷新的问题
使用EF4/5添加数据库视图生成DBContext,如果数据表/对象之间有M-N对应关系,EF对自动添加引用类集合,是Hashset类型.由于不是ObservableCellection类型,在引用类 ...