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 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- Linux下如何卸载HP_LoadGenerator
很简单的一句命令就可以完全卸载! rpm -e LoadGenerator
- CHARINDEX,PATINDEX,STUFF函数
-- CHARINDEX函数 -- 返回字符或者字符串在另一个字符串中的起始位置. -- 语法:CHARINDEX(expression1 , expression2 [,start_location ...
- 【http】生命周期和http管道技术 整理中
httpModules 与 httpHandlers 正在写demo public class Httpext : IHttpModule { public void Dispose() { thr ...
- 【quartz】 入门-配置文件
quartz 启动 NameValueCollection props = (NameValueCollection)ConfigurationManager.GetSection("qua ...
- vagrant在windows下的使用
vagrant在windows下的使用 下载安装 VirtualBox :https://www.virtualbox.org/ 下载安装 Vagrant :http://www.vagrantup. ...
- UI控件tag属性和魔法数字的处理
说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBActio ...
- C# 浅谈委托----温故而知新
先看看委托的概述: •委托类似于 C++ 函数指针,但它们是类型安全的. • 委托允许将方法作为参数进行传递. • 委托可用于定义回调方法. • 委托可以链接在一起:例如,可以对一个事件调用多个方法. ...
- lamada 表达式之神奇的groupby
少说话多干活 先定义一个测试用的实体,接下来会用字段Name进行分组的 public class TestToRun { public string Name { get; set; }//名称 pu ...
- DataGridView 的cell赋值没有线程间访问的限制吗?
1.如下代码,对DataGridView 的cell赋值不会出现线程访问问题,为什么呢? public Form1() { InitializeComponent(); } private void ...
- 新建android项目src和layout文件夹中没有内容的问题
这个问题好像是最新版ADT(ver:23.0.0)才会出现的问题,解决办法也简单,直接把android SDK和ADT的老版本(ver:22.6.2)覆盖安装一次就好了.至于新版为什么会这么设计,现在 ...