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. 法外之徒第一季/全集Braquo迅雷下载

    英文译名 Braquo (第1-2季) (2012-01-05首播)法国.本季看点:<法外之徒>Eddy.Théo.Walter和Roxane私人本是巴黎上塞纳省地区的警察. 因为他们的好 ...

  2. Java中CAS详解

    在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2 ...

  3. [Web前端] 给li设置float浮动属性之后,无法撑开外层ul的问题。

    cp from : https://www.cnblogs.com/cielzhao/p/5781462.html 最近在项目中有好几次遇到这个问题,感觉是浮动引起的,虽然用<div style ...

  4. GIT 使用cherry-pick 重演其他分支的提交

    在使用Git时是否会遇到这样的问题: 你正在使用Git进行版本控制,某天你接着昨天的工作了提交了N个提交,结果在合并远程分支的时候才发现原来你在工作之前没有注意到你要提交的分支状态 结果导致你本来要提 ...

  5. Cognos11只需简单几步创建你的Dashboard

    一.环境 操作系统:win10 数据库   :SQLserver 2008 R2 软件版本:IBM Cognos Analytics 11.0.6 浏览器   :IE 11 二.开始创建仪表板 2.1 ...

  6. Simple XOR Encryption/Decryption in C++ (And Several Other Languages)

    For details on how to implement XOR encryption using Go, see this post. If you are looking for XOR e ...

  7. (转)真正的中国天气api接口xml,json(求加精) ...

      我只想说现在网上那几个api完全坑爹有木有??? 官方的申请不来有木有,还有收费有木有?? 咱这种菜鸟只能用免费的了!!!! http://m.weather.com.cn/data/101110 ...

  8. Oracle整形转字符串to_char()

    使用to_char()将NUMBER转换为字符串: select to_char(AW_PROCESSSTATUS ) as PROCESSSTATUS from A

  9. ThinkPHP3.0启动过程

    以Blog举例载入项目入口文件    D:\wamp\www\Examples\Blog\index.php        定义常量        APP_NAME,Blog        APP_P ...

  10. linux 常用命令--------雪松整理

    linux 常用命令--------雪松整理 博客: http://hi.baidu.com/quanzhou722/blog错误在所难免,还望指正!========================= ...