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.

click to show corner cases.

Corner Cases:

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

  1. 【LeetCode】 -- 68.Text Justification

    题目大意:给定一个数组容器,里面存有很多string: 一个int maxWith.让你均匀安排每一行的字符串,能够尽可能的均匀. 解题思路:字符串+贪心.一开始想复杂了,总觉的题意描述的不是很清楚, ...

  2. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LEETCODE】68、动态规划,medium级别,题目:95、120、91

    package y2019.Algorithm.dynamicprogramming.medium; /** * @ProjectName: cutter-point * @Package: y201 ...

  4. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  5. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  6. 论文阅读(Xiang Bai——【arXiv2016】Scene Text Detection via Holistic, Multi-Channel Prediction)

    Xiang Bai--[arXiv2016]Scene Text Detection via Holistic, Multi-Channel Prediction 目录 作者和相关链接 方法概括 创新 ...

  7. 论文阅读(Xiang Bai——【CVPR2015】Symmetry-Based Text Line Detection in Natural Scenes)

    Xiang Bai--[CVPR2015]Symmetry-Based Text Line Detection in Natural Scenes 目录 作者和相关链接 方法概括 创新点和贡献 方法细 ...

  8. 论文阅读(Xiang Bai——【CVPR2016】Multi-Oriented Text Detection with Fully Convolutional Networks)

    Xiang Bai--[CVPR2016]Multi-Oriented Text Detection with Fully Convolutional Networks 目录 作者和相关链接 方法概括 ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. Caused by: java.net.UnknownHostException: localhost.localdomain: localhost.localdomain的问题解决

    在hosts文件增加如下配置即可,下面的方法适合上面提示的错误,无论是Tomcat问题还是MongoDB等等的问题都可以完美解决. vi /etc/hosts 127.0.0.1 localhost ...

  2. Spring依赖检查

    在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...

  3. mount 和 umount 命令

    参考:http://man.chinaunix.net/linux/mandrake/101/zh_cn/Command-Line.html/fs-and-mntpoints-mount.html 现 ...

  4. 查看系统自带的RPM

    我在home目录下创建了目录mei 1.管理员权限 su 2.进入mei目录 cd /home/mei 3.创建cdrom目录作为挂载点 mkdir cdrom 4.把目录 /dev/cdrom-hd ...

  5. MySql_SQLyog+SQL Assistant实现智能提示

    相信用过sqlserver+SQL Assistant的同学都知道其智能提示多么方便,但是转到mysql后,无论是使用navicat还是webbench都无法实现较好的智能提示效果, 最终在网上找到使 ...

  6. iOS-- UIimageView详解

    原文地址: http://blog.csdn.net/djxiaoyu_haha/article/details/40348377 // (1)创建 UIImageView *imageView = ...

  7. PAT 1033. To Fill or Not to Fill (贪心)

    PAT-A的最后一题,最终做出来了... 是贪心,通过局部最优获得全局最优. 1. 将加油站按距离升序排序 2. 记录当前所在的加油站index,存有的汽油,花费.向后遍历全部 该站可抵达的加油站 3 ...

  8. A4纸的象素分辨率计算[转]

    在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dots Per Inch)指标.在Windows系统的网页打印中默认采用的是96dpi,Mac系统中默认的是72dpi. A4纸张的尺寸是2 ...

  9. Struts2典型应用

    1. Struts2处理表单数据 例1.1 创建Java Web项目,编写一个Action对象,处理对表单提交的数据,模拟实现对指定用户的问候. (1)创建Java Web项目,将Struts的支持类 ...

  10. java springMVC 报400错误问题

    java springMVC 中如果报400错误 很有可能是因为时间转换的问题. 我在项目中就遇到了这个问题,是因为我少引用了一个库,如果是因为时间问题的话添加以下依赖就可以解决. <depen ...