68. 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. "
]
- 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.
Note: Each word is guaranteed not to exceed L in length.
链接: http://leetcode.com/problems/text-justification/
题解:
这是一刷时放弃的第二题。当时可能没有专心读懂题目,所以没做什么尝试就放弃了,现在来试着解一下。
读完题目以后,想法是有几个点我们要分个击破:
- 首先要判断每一行能容纳多少单词,每两个单词间要有一个空格
- 在得到单词容量和必须的空格数后,我们计算剩余长度是多少,然后根据单词数目来evenly distributed剩余的空格,有点像Round Robin
- 找到潜在的边界情况并处理, 返回结果。
记录下第一次过的长code,修修补补了很久。 步骤如下:
- 首先我们初始化
- 一个结果集res, 一个StringBuilder sb用来做缓冲
- 一个int值widthLeft用来记录还剩下多少长度可以使用
- 一个int值lineWordCount 用来记录这一行已经使用了多少单词,为的是以后计算单词间有多少个slot要填
- 从头遍历给定数组
- 先取出current word并且求出word length
- 假如wordLen + sb.length() <= maxWitdh,说明我们可以将这个单词添加入当前行,进行以下步骤:
- 我们先把单词加入到这一行, sb.append(word)
- 我们再append一个空格" "
- 此时lineWordCount++, 更新剩余长度,减掉单词长度和空格长度widthLeft -= (wordLen + 1)
- 接下来要对widthLeft进行判断
- 假如widthLeft > 0, 我们不理会,继续处理下面的单词
- 假如widthLeft < 0,这时说明我们最后的空格多加了。我们需要把当前sb.toString()进行一下trim()然后加入到结果集中,并且重置witdhLeft,sb,以及lineWordCount
- 否则这时widthLeft == 0。我们需要判断lineWordCount是否为1
- 假如lineWordCount等于1,我们把sb加入到结果集合中,重置变量们
- 否则,我们需要把最后的这一个空格抹去,在sb的第一个空格处再添加一个空格,sb.toString()加入到结果集中,并且重置变量们
- 第二种情况当前wordLen + sb.length() > maxWitdh,我们不能将此单词加入当前行,需要将当前行处理之后再把这个单词加入新一行,这里也分两种情况
- 假如lineWordCount = 1,这时候我们需要将这个单词后面的空格补齐,一直补到maxWitdh
- 否则,我们需要处理以下步骤
- 先抹掉sb中的最后一个空格,计算出一共需要多少个空格 totalSpaceAdd = widthLeft + 1, 以及有多少slot需要补空格, slot = lineWordCount - 1
- 接下来,我们根据totalSpaceAdd和slot的关系,对sb中的每一个空格处进行补空格
- 最后将结果加入到结果集中,并且重置变量。假如没有处理当前单词,则i--倒退回去处理,否则要多写几行代码处理当前单词。
- 在结束的时候要判断一下是否sb.length依然大于0,否则我们还要将这最后一行进行处理。假如sb.length() > 0,按照题目要求我们在其后面补空格直到结束。
需要好好简化code,学习大牛们怎么写的。
Time Complexity - O(n), Space Complexity - O(n)
public class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
StringBuilder sb = new StringBuilder();
int widthLeft = maxWidth;
int lineWordCount = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
int wordLen = word.length();
if (wordLen + sb.length() <= maxWidth) {
sb.append(word);
sb.append(" ");
widthLeft -= (wordLen + 1);
lineWordCount++;
if (widthLeft < 0) {
res.add(sb.toString().trim());
widthLeft = maxWidth;
sb.setLength(0);
lineWordCount = 0;
} else if (widthLeft == 0) {
if (lineWordCount != 1) {
sb.setLength(sb.length() - 1);
for (int k = 0; k < sb.length(); k++) {
char c = sb.charAt(k);
if (c == ' ') {
sb.insert(k, ' ');
break;
}
}
}
res.add(sb.toString());
widthLeft = maxWidth;
sb.setLength(0);
lineWordCount = 0;
}
} else {
if (lineWordCount == 1) {
while (widthLeft > 0) {
sb.append(" ");
widthLeft--;
}
} else {
sb.setLength(sb.length() - 1);
int totalSpaceToAdd = widthLeft + 1;
int slots = lineWordCount - 1;
String s = sb.toString();
sb.setLength(0);
for (int k = 0; k < s.length(); k++) {
char c = s.charAt(k);
sb.append(c);
if (c == ' ') {
if (totalSpaceToAdd <= 0) {
continue;
}
int spaceToAdd = 0;
if (totalSpaceToAdd >= slots) {
spaceToAdd = (int) Math.ceil((double)totalSpaceToAdd / slots);
totalSpaceToAdd -= spaceToAdd;
slots--;
} else {
spaceToAdd = 1;
totalSpaceToAdd--;
}
while (spaceToAdd > 0) {
sb.append(' ');
spaceToAdd--;
}
}
}
}
widthLeft = maxWidth;
res.add(sb.toString());
sb.setLength(0);
lineWordCount = 0;
i--;
}
}
if (sb.length() != 0) {
while (widthLeft > 0) {
sb.append(" ");
widthLeft--;
}
res.add(sb.toString());
}
return res;
}
}
精炼和更新:
改写了一下,把逻辑理顺了一点点。下面是改写的思路:
- 一开始仍然是初始化, 初始化结果集合res, 一个用来处理当前行的StringBuider sb,以及一个lineWordCount = 0
- 接下来遍历数组,先计算出当前单词word以及它的长度, 接下来我们主要分三种情况来考虑
- wordLen + sb.length() == maxWidth,这时我们找到一个结果,在sb中append当前单词,然后把sb.toString()加入到结果集合中,重置sb和lineWordCount
- wordLen + sb.length() < maxWidth,这时候说明我们仍然可以在当前行里塞单词,我们先append(word),再append一个空格" ", 更新lineWordCount++
- 否则说明当前wordLen + sb.length() > maxWidth,这时候我们必须对当前行sb进行处理,然后才可以继续后面的操作。对这种情况我们又可以分为两种子情况:
- 当lineWordCount == 1,这时候这一行只有一个单词,我们只需要在当前行sb的后面补足空格,直到补充到maxWidth为止
- 否则lineWordCount > 1,这时这一行有多个单词,我们执行一个分拆出来的函数distributeSpaces来把单词中的空格平均分配
- 经过上两步求出了当前行sb之后,我们可以把sb加入到结果集中,重置sb和lineWordCount,因为我们并没有处理当前单词,所以要减少index i,用i--来重新处理当前单词
- 主循环结束之后我们处理最后一行,根据题目意思,在sb中补空格直到maxWidth,然后将其加入到结果集中。
- 最后返回结果
关于辅助函数distributeSpaces,主要逻辑分为以下几个步骤:
- 当一行有多个单词的时候,我们需要把多余的空格均匀分配到每个已有的空格slot里,假如不够分,则尽量放到左边的slot里,这就有了我们的函数
- 我们的函数包括了上面主逻辑第3步的1和2小步,分为lineWordCount == 1时和lineWordCount > 1两种情况考虑
- 当lineWordCount == 1,这时候这一行只有一个单词,我们只需要在当前行sb的后面补足空格,直到补充到maxWidth为止
- 否则lineWordCount > 1,这时这一行有多个单词,我们执行一个分拆出来的函数distributeSpaces来把单词中的空格平均分配
- 这里我们首先计算出一共要分配多少个空格,这里newSpaceTotal = maxWidth - sb.length() + 1, 因为之前我们在每个单词后面都加入了一个空格,所以计算时要把这个考虑进去
- 我们有多少个slot可以插入, 因为每两个单词中间就算一个slot,所以slots = lineWordCount - 1
- 接下来我们先把sb转换为String s,再重置sb.setLength(0), 对于s, 从0到 s.legnth() - 2进行遍历 (不遍历最后一个空格)。
- 假如当前的字符为c,我们在sb中append(c), 假如c为空格的话,我们需要进行额外的判断:
- 当newSpaceTotal <= 0时, 用continue跳过
- 设置一个int spaceToAdd = 0,代表当前要加入的空格数
- 当newSpaceTotal > slots时, 我们计算这个slot可以添加多少个空格
- spaceToAdd = (int) Math.ceil((double) newSpaceTotal / slots), 这里要向上取整
- newSpaceTotal -= spaceToAdd
- slot--
- 否则slots数目大于等于newSpaceTotal, 我们最多每个slot可以分配一个space,所以
- spaceToAdd = 1
- newSpaceTotal--
- 接下来根据spaceToAdd的数目,我们在sb里面append(" ")。
- 当newSpaceTotal > slots时, 我们计算这个slot可以添加多少个空格
public class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
StringBuilder sb = new StringBuilder();
int lineWordCount = 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
int wordLen = word.length();
if (wordLen + sb.length() == maxWidth) {
sb.append(word);
res.add(sb.toString());
sb.setLength(0);
lineWordCount = 0;
} else if (wordLen + sb.length() < maxWidth) {
sb.append(word);
sb.append(" ");
lineWordCount++;
} else {
distributeSpaces(sb, maxWidth, lineWordCount);
res.add(sb.toString());
sb.setLength(0);
lineWordCount = 0;
i--;
}
}
if (sb.length() != 0) {
while (sb.length() < maxWidth) {
sb.append(" ");
}
res.add(sb.toString());
}
return res;
}
private void distributeSpaces(StringBuilder sb, int maxWidth, int lineWordCount) {
if (lineWordCount == 1) {
while (sb.length() < maxWidth) {
sb.append(" ");
}
} else {
int newSpaceTotal = maxWidth - sb.length() + 1;
int slots = lineWordCount - 1;
String s = sb.toString();
sb.setLength(0);
for (int k = 0; k < s.length() - 1; k++) {
char c = s.charAt(k);
sb.append(c);
if (c == ' ') {
if (newSpaceTotal <= 0) {
continue;
}
int spaceToAdd = 0;
if (newSpaceTotal > slots) {
spaceToAdd = (int)Math.ceil((double)(newSpaceTotal) / slots);
newSpaceTotal -= spaceToAdd;
slots--;
} else {
spaceToAdd = 1;
newSpaceTotal--;
}
while (spaceToAdd > 0) {
sb.append(" ");
spaceToAdd--;
}
}
}
}
}
}
Reference:
https://leetcode.com/discuss/13610/share-my-concise-c-solution-less-than-20-lines
http://www.cnblogs.com/springfor/p/3896168.html
https://leetcode.com/discuss/30857/share-my-2-ms-30-lines-solution
https://leetcode.com/discuss/48959/easy-java-implementation
https://leetcode.com/discuss/20896/easy-understanding-solution
68. Text Justification的更多相关文章
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 *HARD*
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 68. Text Justification (JAVA)
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- 机器学习实战——k-邻近算法:约会网站
1.kNN 算法 算法说明: set<X1,X2……Xn> 为已知类别数据集,预测 点Xt 的类别: (1)计算中的set中每一个点与Xt的距离 (2)按距离增序排列 (3)选择距离最小的 ...
- Microsoft Azure Powershell 获取Azure-Location
首先要切换至AzureResourceManager模式下 http://www.cnblogs.com/SignalTips/p/4110790.html 国际版Get-AzureLocation ...
- java的JVM机制
1.jre:java运行环境 提供一个JVM和一些基础类库.2.只安装jre以后,机器就具备了运行java程序的条件.但是不具备开发java程序的条件.安装JDK以后,在c:/program file ...
- IIS支持PHP
1. 解压php-5.2.6.zip到D:\php5,找到php.ini-dist改名为php.ini并将它放到C:\WINDOWS目录下. 2. 将D:\ php5目录下的libmcrypt.dll ...
- python之类定义
<python基础教程>第7章说python中的类定义: 1. 要么声明__metaclass__=type 2. 要么继承object. 但是直接定义下类, 也没报错: >> ...
- spring-mysqlclient开源了
https://github.com/risedragon/spring-mysqlclient/wiki/spring-mysqlclient-user-guide 开源了一个项目,总结了几年的数据 ...
- [转] c和python利用setsockopt获得端口重用
假如端口被socket使用过,并且利用socket.close()来关闭连接,但此时端口还没有释放,要经过一个TIME_WAIT的过程之后才能使用.为了实现端口的马上复用,可以选择setsockopt ...
- 你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?
php: 一般是在你要检查的代码开头记录一个时间,结尾记录一个时间.取差值, 数据库SQL的效率 sql的explain(mysql),启用slow query log记录慢查询. 通常还要 ...
- iOS常见问题(5)
一.注意将之前storyboard中控制器删除之后,拖入一个新的控制器的时候,stroyboard中控制器的class也要重新填入自己想要展示的控制器,告诉stroyboard去加载哪个控制器. 二. ...
- pure.css
注释中address是纠正的意思 等价于correct/*! Pure v0.5.0 Copyright 2014 Yahoo! Inc. All rights reserved. Licensed ...