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 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.
- 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 文本左右对齐的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Text Justification,文本对齐
问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", ...
- [Leetcode] text justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Text Justification 实现两端对齐功能
实现office word中的两端对齐功能. 只有个单词时,右边补齐空格.最后一行每个词间一个空格,整下的空格右边补齐.给定字符串,和每行的字符数L.进行两端对齐输出. 我的思路是写一个函数,给定相应 ...
- [Swift]LeetCode68. 文本左右对齐 | Text Justification
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- [MIT6.006] 20. Daynamic Programming II: Text Justification, Blackjack 动态规划II:文本对齐,黑杰克
这节课通过讲解动态规划在文本对齐(Text Justification)和黑杰克(Blackjack)上的求解过程,来帮助我们理解动态规划的通用求解的五个步骤: 动态规划求解的五个"简单&q ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
随机推荐
- USB OTG简单介绍、与普通USB线的差别
USB有三类接口A类接口 -----------最常见的扁平接口,四芯 VCC GND D+ D- B类接口 ...
- IIS回收后首次访问慢问题
禁用时间间隔回收 设置为0 然后设置指定时间回收 0:00:00 然后设置脚本 @echo off @echo 正在关掉所有的IE进程(需要设置默认浏览器是IE) taskkill /im iexpl ...
- 在EditText中限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- JavaScript 中的异常处理
考虑到 JS 中的错误可比服务器端的代码产生的错误要多得多,并且还难以发现及修正,所以 JS 代码必须有异常处理以及全局一场处理. try { //这段代码从上往下运行,其中任何一个语句抛出异常该代码 ...
- error “Device supports x86, but APK only supports armeabi-v7a”
checkout the build.gradle from module:app. It turns out that there's a such config: ndk { abiFilters ...
- [转]linux最新分区方案
FROM : http://www.cnblogs.com/chenlulouis/archive/2009/08/27/1554983.html 我的服务器是500G.最重要的是/var分区一定要大 ...
- XML和JSON优缺点
<1>.XML的优点 A.格式统一,符合标准: B.容易与其他系统进行远程交互,数据共享比较方便.<2>.XML的缺点 A.XML文件庞大,文件格式复杂,传输占带宽: B.服务 ...
- head first--------------state pattern
head first----------浅谈状态模式 状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类 实现代码如下: package com.cl ...
- 基于S3C2440的嵌入式Linux驱动——看门狗(watchdog)驱动解读
本文将介绍看门狗驱动的实现. 目标平台:TQ2440 CPU:s3c2440 内核版本:2.6.30 1. 看门狗概述 看门狗其实就是一个定时器,当该定时器溢出前必须对看门狗进行"喂狗“,如 ...
- 使用jQuery获取radio/checkbox组的值的代码收集
<!-- $("document").ready(function(){ $("#btn1").click(function(){ $("[na ...