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 exactly L 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.

  分析: 主要注意两个地方。一, 正常的句子单词之间的空格数目是一;二,最后一行句子按正常处理

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> res;
if(words.size() < ) return res; int start , end, len , evenSpace, bonus; end = ;
while(true){ len = ;
for(start = end; end < words.size(); ++end){
if(len +(end - start) + words[end].length() > L) break;// normal sentence has one space between words
len += words[end].length();
}
end--; if(end == start){ // only one word
string line = words[end];
line.append(L - len, ' ');
res.push_back(line);
}else{ // multiply words evenSpace = (L - len)/(end - start);
bonus = L -len - evenSpace*(end - start);
bool isLastLine = (end == words.size() -);
if(isLastLine){
evenSpace = ;
bonus = ;
} string line = words[start];
for(int i = start + ; i <= end; ++i)
{
int space = evenSpace;
if(bonus > )
{
--bonus;
++space;
}
line.append(space,' ');
line.append(words[i]);
} if(isLastLine){
line.append(L-len - (end - start), ' ');
} res.push_back(line);
} end++;
if(end == words.size()) break;
} return res;
}
};

LeetCode_Text Justification的更多相关文章

  1. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  2. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  3. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  4. LeetCode(68) Text Justification

    题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...

  5. LeetCode:Text Justification

    题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...

  6. Java for LeetCode 068 Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  7. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

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

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

  9. juce Justification 分析

    很简单的一个类,一个rect放置在另一个rect中如何放置.只是没有考虑边距等,估且认为是在外层作考虑吧.然后认为是外框比内框大,所以外层怕是要进行检查才行 #ifndef JUCE_JUSTIFIC ...

随机推荐

  1. 【转】Java 集合系列16之 HashSet详细介绍(源码解析)和使用示例--不错

    原文网址:http://www.cnblogs.com/skywang12345/p/3311252.html 概要 这一章,我们对HashSet进行学习.我们先对HashSet有个整体认识,然后再学 ...

  2. delphi编写dll心得, 谢谢原作者的分享。转

    delphi编写dll心得 1.每个函数体(包括exports和非exports函数)后面加 'stdcall;', 以编写出通用的dll2.exports函数后面必须加'export;'(放在'st ...

  3. Java---设计模块(工厂方法)

    ★ 场景和问题 Java程序开发讲究面向接口编程,隐藏具体的实现类,可是如何得到接口呢? 工厂类的命名规范:***Factory 单例工厂方法的命名规范:getInstance() 工厂的本质是&qu ...

  4. 为什么选择Premium Fabric Shanghai

    展商首页 为什么选择Premium Fabric Shanghai

  5. velocity序列动画

          结合上次提到的velocity的UI Pack存在一下问题: 动画名称过长,语意性差 使用UI Pack的动画,loop属性会失效 无法监听动画完成时机        我这里想到了一种解决 ...

  6. Hadoop2.4.1 64-Bit QJM HA and YARN HA + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA Install

    Hadoop2.4.1 64-Bit QJM HA and YARN HA Install + Zookeeper-3.4.6 + Hbase-0.98.8-hadoop2-bin HA(Hadoop ...

  7. DynamicMBean(Java SE 6 新特性: JMX 与系统管理)

    Dynamic MBean 是一种在运行时定义其管理接口的 MBean.例如,配置 MBean 可以通过解析 XML 文件来确定它所公开的属性名称和类型. 任何实现 DynamicMBean 接口的类 ...

  8. IOS设计模式学习(19)策略

    1 前言 面向对象软件设计中,我们可以把相关算法分离为不同的类,成为策略.与这种做法有关的一种设计模式成为策略模式. 2 详述 2.1 简述 策略模式中得一个关键角色是策略类,它为所有支持的或相关的算 ...

  9. 视图的touch事件的传播控制

    在视图控制器类中.self.view中会包含多个多层的自己定义视图. 我自己定义了一个uicollectionview类,名称为gridview,又自己定义了一个uicollectionviewcel ...

  10. Baidu与Google地图API初探

    前天周六,有个好友过来玩,他说想在他的站点中加入地图导航模块,但不知道选择哪个第三方Map API 在网上查了下Baidu.Google.QQ和MapBar等4种Map API(都是採用JS开放API ...