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 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- Global::pickSpecificClass_DNT
/*************************************************** Created Date: 13 Jul 2013 Created By: Jimmy Xie ...
- Freemarker例子
1.引入架包 2.写ftl文件 3.代码 hello.ftl 你好啊,${hello},今天你的精神不错! if else 语句测试 <#if num gt 18><#-- 不使用 ...
- JNI 学习笔记
JNI是Java Native Interface的缩写,JNI是一种机制,有了它就可以在java程序中调用其他native代码,或者使native代码调用java层的代码.也 就是说,有了JNI我们 ...
- Infobright高性能数据仓库
1. 概述 Infobright是一款基于独特的专利知识网格技术的列式数据库.Infobright简单易用,快速安装部署,使用中无需复杂操作,能大幅度减少管理工作:在应对50TB甚至更多数据量进行多 ...
- MySQL 5.6.26源码安装
5.6.26源码安装包:http://pan.baidu.com/s/1kUl44WRcmake安装包链接:http://pan.baidu.com/s/1c0LuwJA 操作系统版本:CentOS ...
- Linq小技巧
遍历集合ForEach: listAll.Items.Clear(); List<Users> list = DP.UsersDAO.GetInfoList(); list.ForEach ...
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
问题描述: c#程序本机运行没有问题,部署到正式环境后报错: System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本 思考: 难道真是客户端的问题吗? ...
- html+css学习笔记 5[表格、表单]
表格 -- 默认样式重置 表格标签: table 表格 thead 表格头 tbody 表格主体 tfoot 表格尾 tr 表格行 th 元素定义表头 ...
- 1066: [SCOI2007]蜥蜴 - BZOJ
Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平 ...
- UVA 11858 Frosh Week 逆序对统计
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/H Frosh Week Time Limit:8000MSMemory Limi ...