(这道题还是不要看这篇旧博文比较好)新博文地址:[leetcode]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 L 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. "
]
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.
Corner Cases:
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.
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 L 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. "
]
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.
Corner Cases:
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.
怎么说呢,这道题不难,但是略烦,毫无疑问的是,该题肯定可以用递归实现。
算法思想:
1. 首先需要从数组中挑出几个可以组成一个String的元素(这有两种情况,是不是最后一行)
2. 如果是最后一行,比较容易,将短缺的长度用空格补充即可
3. 如果不是最后一行,又有几种情况,
3.1. 一个单词组成的行,只需要填充后面
3.2 多个单词组成,但是短缺的长度可以均匀填充
3.3 多个单词组成,但是短缺的长度不可以均匀填充(3.2和3.3一并处理)
4 递归处理数组剩下的部分
代码较长,但是不知道还能怎么优化时间复杂度,当然,可以从代码的简洁度o(╯□╰)o
List<String> result = new ArrayList<String>(); public List<String> fullJustify(String[] words, int L) { generateEachLine(words, L); return result; } private void generateEachLine(String[] words, int l) { if (words == null || words.length == 0) { result.add(""); return; } StringBuilder sb = new StringBuilder(); String[] wordsPerLine = new String[words.length]; wordsPerLine[0] = words[0]; int totalLength = wordsPerLine[0].length(), i = 1, spaceCount = 0; for (; i < words.length; i++) { wordsPerLine[i] = words[i]; int length = wordsPerLine[i].length() + 1; totalLength += length; if (totalLength <= l) {// 如果长度不超过l,可以加入该行 spaceCount++; } else {//超过l,就应该放在下一行处理 totalLength -= length; i--; break; } } sb.append(wordsPerLine[0]); if (i == words.length) {//i== length最后一行 if (totalLength <= l) {//如果有短缺,用空格补足 for (int j = 1; j < i; sb.append(" ").append(wordsPerLine[j]), j++); for (int j = 0; j < l - totalLength; sb.append(' '), j++); result.add(sb.toString()); return; } } else {// 不是最后一行 if (spaceCount == 0) {//如果只有一个单词 for (int j = 0; j < l - totalLength; sb.append(' '), j++); } else { int spaceNeed2Add = l - totalLength; int spaceLeft = spaceNeed2Add % spaceCount; int spaceAverge = spaceNeed2Add / spaceCount; for (int j = 1; j <= i; j++) {//这里将3.2 3.3一起处理了 for (int k = 0; k < spaceAverge; k++, sb.append(' ')); if (spaceLeft > 0) { sb.append(' '); } spaceLeft--; sb.append(' ').append(wordsPerLine[j]); } } result.add(sb.toString()); String[] left = new String[words.length - i - 1];//这里很别扭,不知道咋优化 for (int j = i + 1; j < words.length; left[j - i - 1] = words[j], j++); generateEachLine(left, l); } }
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
leetcode-text 92.Reverse Linked List II Runtime: 4 ms, faster than 67.04% of C online submissions for Reverse Linked List II. Memory Usage: 6.9 MB, less than 100.00% of C online submissions for ...
大佬的leetcode刷题笔记(c++版本)
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题
LeetCode 刷题笔记
(C++)LeetCode刷题题解答案
LeetCode 刷题
leetcode全套解答python版本。包括更新到10月份的的leetcode