68. Text Justification一行单词 两端对齐
[抄题]:
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
wordscontains at least one word.
Example 1:
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.
Example 3:
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
粘贴的时候遵循【第一个单词】+【空格】+【下一个单词】,先用空格占位置 空格在前面,否则空格在后面,不能做到最后还是单词:
["This is an ","example of ","text ","justification. "]
["This is an","example of text","justification. "]
[思维问题]:
没思路
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
每一行中,用w数单词,数空格:初始化空格、更新空格,贴空格:贴正常的空格、剩下的空格贴最后面
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 贴单词的时候,w_w结构,别忘了把最后一个单词贴上
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
写这么长,无非就是每一行数空格、贴空格
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
//ini
List<String> res = new ArrayList<>();
//cc
if (words == null || words.length == 0) return res;
if (maxWidth == 0) res.add("");
//for each line i, count w, ini space, update space, add normal space: w_w, rmn space
for (int i = 0, w; i < words.length; i = w) {
//count word
int len = - 1;
for (w = i; w < words.length && len + words[w].length() + 1 <= maxWidth; w++) {
len += words[w].length() + 1;
}
//ini space
int normalSpace = 1;
int extraSpace = 0;
int gaps = w - i - 1;
//update spae
if (w != i + 1 && w != words.length) {
normalSpace = (maxWidth - len) / gaps + 1;
extraSpace = (maxWidth - len) % gaps;
}
//add normal space: w_w in one line
StringBuilder sb = new StringBuilder(words[i]);
//each word
for (int j = i + 1; j < w; j++) {
for (int k = 0; k < normalSpace; k++) {
sb.append(' ');
}
if (extraSpace > 0) {
sb.append(' ');
extraSpace--;
}
//append the last
sb.append(words[j]);
}
//append rmn to the tail
int rmnSpaces = maxWidth - sb.length();
while (rmnSpaces > 0) {
sb.append(' ');
rmnSpaces--;
}
res.add(sb.toString());
}
return res;
}
}
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 文本对齐
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 width maxWidth, format the text such that each line has exactly maxWid ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 (JAVA)
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
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
随机推荐
- POJ2503:Babelfish
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:http://poj.org/problem?id=2503 \(Trie ...
- C/S模式与B/S模式的详细介绍
网络程序开发的两种计算模式--C/S模式与B/S模式.两种各有千秋,用于不同场合. C/S适用于专人使用,安全性要求较高的系统: B/S适用于交互性比较频繁的场合,容易被人们所接受,倍受用户和软件开发 ...
- laravel里面的控制器笔记
看了下教程,总结了下,大概分两种 一般的controller restful的controller 单独绑定action的route为 Route::get('user/{id}', 'UserCon ...
- android视频处理相关资料
<开源>基于Android的点对点视频通信/RTSP/RTP/H.264 http://blog.csdn.net/cazicaquw/article/details/8650543 历经 ...
- 【转】使用 JMeter 完成常用的压力测试
本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web 服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项. ...
- Appium+python自动化23-Android夜神模拟器
前言 Android SDK虽然也自带了模拟器,但是那速度会让你怀疑人生,并且不稳定经常卡死异常.夜神模拟器可以说是android模拟器里面的一个神器. 环境安装 1.官网下载地址:https://w ...
- 初学者手册-Sublime Text常用快捷键
Alt + F3 :找出当前文档中所有被划选的词语,若文档很大的话,可能会导致Sublime Text崩溃. Ctrl + kkk :删除当前行光标至行尾的所有内容. End: 光标跳至行尾. Hom ...
- 关于git rebase的相关讲解
http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin" ...
- MySQL 5.7 坑爹参数 – log_timestamps
官网原话: This variable was added in MySQL 5.7.2. Before 5.7.2, timestamps in log messages were written ...
- 【BZOJ】1030: [JSOI2007]文本生成器(AC自动机+dp)
题目 传送门:QWQ 传送到洛谷QWQ 分析 我一开始也不会做这题的,后来看了很多网上的题解,终于AC了.(我好菜啊) 主要参考:传送门QWQ 直接搞非常麻烦,反正我是不会做.于是考虑求反,即求有多少 ...