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 Lcharacters.

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.

首先要做的就是确定每一行能放下的单词数,这个不难,就是比较n个单词的长度和加上n - 1个空格的长度跟给定的长度L来比较即可,找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数,是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,就要在每个单词后面插入这些空格,这里有两种情况,比如某一行有两个单词"to" 和 "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to   a",如果是最后一行,则应该输出 "to a  ",所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里,以此类推,

class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L){
vector<string> result;
// input validation
if(words.empty()) return result; // param @ i 遍历单词的下标临时变量
// param @ k 校正后每行单词个数
// param @ l 每行字符(除空格)个数
// 每次遍历构造一行 将单词检索进新数组
for(int i = , k, l; i < words.size(); i += k){
// 查询单词不能越界 并且每行增添的单词需要满足要满足中间能插入空格
//
for(k = , l = ; k+i < words.size() && l + words[i+k].size()<= L-k;
l += words[i+k].size(), ++k); // 构造新行
string temp = words[i]; // 每行首单词
for(int j = ; j < k-; ++j){
int blanks = (L-l)/(k-) + (j < (L-l)%(k-));
// 已经是最后一行单词,要求左对齐,每个单词之间一个空格,剩下空格放在最后
if(i+k >= words.size()){
temp += " ";
}
else temp += string(blanks, ' ');
temp += words[i+j+];
}
// 补全空格
temp += string(L-temp.size(), ' ');
result.push_back(temp);
}
return result;
}
};

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. Text Justification,文本对齐

    问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", ...

  3. [Leetcode] text justification 文本对齐

    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 characters ...

  5. Text Justification 实现两端对齐功能

    实现office word中的两端对齐功能. 只有个单词时,右边补齐空格.最后一行每个词间一个空格,整下的空格右边补齐.给定字符串,和每行的字符数L.进行两端对齐输出. 我的思路是写一个函数,给定相应 ...

  6. [Swift]LeetCode68. 文本左右对齐 | Text Justification

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  7. [MIT6.006] 20. Daynamic Programming II: Text Justification, Blackjack 动态规划II:文本对齐,黑杰克

    这节课通过讲解动态规划在文本对齐(Text Justification)和黑杰克(Blackjack)上的求解过程,来帮助我们理解动态规划的通用求解的五个步骤: 动态规划求解的五个"简单&q ...

  8. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  9. 68. Text Justification一行单词 两端对齐

    [抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly  ...

随机推荐

  1. 闪电侠第四季/全集The Flash迅雷下载

    闪电侠第四季>(The Flash Season 4)是DC娱乐和华纳联手CW电视台制作的真人超级英雄系列剧,是美剧<闪电侠>系列的第四季. 该季于2017年10月10日在美国CW电 ...

  2. ios之申请后台延时执行和做一个假后台的方法

    转自:http://sis hu ok.com/forum/blogCategory/showByCategory.html?categories_id=138&user_id=10385   ...

  3. Tornado开发技巧,简单了解tornado

    tornado基础入门(一)——简单了解tornado 参考:http://demo.pythoner.com/itt2zh/ch1.html tornado是一个轻量级的web框架,是一个用pyth ...

  4. 摩拜单车模式优于OFO双向通信才能被认可

    马化腾 :摩拜单车模式优于OFO双向通信才能被认可 2017-06-20 00:12 最近共享单车里最头条的新闻是 悟空单车宣布退出竞争,并全部退还投资款和押金以及余额.运营才5个月,成为第一家倒下的 ...

  5. [转]Linux awk 命令 说明

    From : http://blog.csdn.net/tianlesoftware/article/details/6278273 一.  AWK 说明 awk是一种编程语言,用于在linux/un ...

  6. Eclipse Debug提示source not found解决方案

    场景:我在引用的jar包的某个class文件的某一行代码打了个断点,当然我是已经指定了source源码包路径,否则我是查看不了class文件的源码更不用说打断点了,但是在debug模式下,却提示&qu ...

  7. Java switch 枚举

    Switch中能够使用int.byte.short,char,Enum,String.当中Enum为1.5之后新增特性,String为java8新增特性.本文介绍怎样在Switch中使用Enum类型. ...

  8. Spring配置中的"classpath:"与"classpath*:"的区别研究

    概念解释及使用场景: classpath是指WEB-INF文件夹下的classes目录. 通常我们一般使用这种写法实在web.xml中,比如spring加载bean的上下文时,如下: <!--系 ...

  9. api重复引用导致的诡异问题排查

    api重复引用导致的诡异问题排查 最近一个项目上线前开发环境.测试环境都能正常打包并运行.然而到了准生产环境和生产环境则报一些诡异的错误信息: [INFO] --------------------- ...

  10. osg for android学习之一:windows下编译(亲测通过)【转】

    1. 首先需要一个OSG for android的环境 (1)NDK 现在Eclipse 对NDK已经相当友好了,已经不需要另外cygwin的参与,具体可以参考 Android NDK开发篇(一):新 ...