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. AS3使用Json 传复杂数据 -------- 用数组而不是向量

    package { public class Person { private var name:String; public function get Name():String { return ...

  2. SSM+Maven(教程二):Idea快速入门SSM+Maven框架。

    快速入门须知 这篇文章,直接应用已经搭建好的SSM框架.一般在公司里面,考虑框架的搭建.封装等问题,都由研发经理或者架构师完成,所以对于刚入门的小白来说,在去搭建整合花费的时间会很多很多.对于理解能力 ...

  3. 使用Win2D在UWP程序中2D绘图(一)

    在新的Windows UWP程序中,引入了一个新的API库: Win2D.它是一个d2d的封装,可以直接使用C#来快速实现高效2D绘图了.这个API虽然在Win8.1时代就开始着手开发了,但最近才完善 ...

  4. Java并发教程(Oracle官方资料)

    计算机的使用者一直以为他们的计算机可以同时做很多事情.他们认为当其他的应用程序在下载文件,管理打印队列或者缓冲音频的时候他们可以继续在文字处理程序上工作.甚至对于单个应用程序,他们任然期待它能在在同一 ...

  5. 视频运行库AVICAP32.DLL说明收藏

    视频运行库AVICAP32.DLL说明收藏2008-09-28 09:04 // ----------------------------------------------------------- ...

  6. 在JavaScript文件中读取properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

  7. java设计模式5--原型模式(Prototype)

    本文地址:http://www.cnblogs.com/archimedes/p/java-prototype-pattern.html,转载请注明源地址. 原型模式 用原型实例指定创建对象的种类,并 ...

  8. HTTP客户端都应该支持的五个特性

    在我看来,HTTP客户端必须要支持如下五个特性. 缓存 最后修改时间的检查 ETags 压缩 重定向 缓存返回的头如下: Cache-Control: max-age=, public Expires ...

  9. 如何快速访问MSDN某一个类或方法的帮助文档

    如何快速访问MSDN某一个类或方法的帮助文档? 我一般都是在Google上搜索的如"string msdn",而不是在Msdn上直接查找(你不可能知道所有的类或方法的完整命名空间) ...

  10. angularjs中ng-show的使用

    ng-show 指令在表达式为 true 时显示指定的 HTML 元素,否则隐藏指定的 HTML 元素.下面是示例: <!doctype html> <html lang=" ...