(这道题还是不要看这篇旧博文比较好)新博文地址:[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); } }
相关推荐
javascript js_leetcode题解之68-text-justification.js
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 ...
vs code LeetCode 插件
《Python版LeetCode题解全集详解》 LeetCode是一个广受欢迎的在线编程挑战平台,致力于帮助程序员提升技能,特别是面试准备。这个压缩包“lc-all-solutions-master”包含了使用Python语言解决LeetCode所有问题的...
《使用leetcode-editor在IDE中进行LeetCode练习的全方位指南》 LeetCode是一个广受欢迎的在线编程练习平台,它提供了一系列的算法题目供程序员们提升技能。对于习惯在集成开发环境(IDE)中工作的开发者来说,将...
"IDEA leetcode-editor插件"就是将这两者结合的工具,允许用户在IDEA中直接进行LeetCode的编程挑战,无需离开开发环境,提高了刷题的便捷性。 该插件的主要功能包括: 1. **离线模式**:在无法访问LeetCode官网的...
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
(C++)LeetCode刷题题解答案
leetcode刷题, 直接用leetcode的分类方式.
### LeetCode中文版知识点概述 #### 一、LeetCode简介与使用 LeetCode是一个全球知名的在线编程学习平台,主要提供各种算法题目供开发者练习。它不仅涵盖了基础数据结构和算法训练,还包括了大量的面试真题,是...
LeetCode面试笔试题
基于Python实现的LeetCode爬虫爬取LeetCode题目描述和提交的代码.zip ## 特点 - 支持爬取题目列表,保存为本地CSV/Excel文件。 - 支持爬取题目描述,保存为本地HTML文件。 - 支持爬取用户提交的代码,保存为如_.py...
LeetCode-Swift, 快速LeetCode解决方案 LeetCodeLeetCode在线判断是一个包含很多收费算法的网站。 them Google Google Google Google LinkedIn this this repo 。 请免费参考并收费STAR以支持这个 repo,
- Text Justification:该题目要求对文本进行左右对齐处理,是字符串处理和编程思维的综合应用。 接下来,我们看下Amazon的题目集合: - Validate Binary Search Tree:验证给定的二叉树是否为有效的二叉搜索树...
leetcode 习题集, leetcode 官网出品,包含基本的算法
《LeetCode经典题目全解析》是一份由编程高手精心编撰的文档,旨在全面解析LeetCode平台上众多的算法挑战题目。LeetCode作为一个知名的在线编程练习平台,汇集了各种难度级别的编程题目,涵盖数据结构、算法、设计...
LeetCode 400题目 Java版本 (LeetCode is the platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.)
Source file for LeetCode Permutations Problem
《LeetCode Editor 7.4:提升编程技能的利器》 在编程学习和实践中,LeetCode 已经成为了程序员们磨炼算法、提升编程技能的重要平台。为了方便开发者更高效地进行刷题,LeetCode 提供了官方编辑器插件——LeetCode ...
leetcode算法。本书包含了 LeetCode Online Judge(http://leetcode.com/onlinejudge) 所有题目的答案,所有 代码经过精心编写,编码规范良好,适合读者反复揣摩,模仿,甚至在纸上默写。 全书的代码,使用 C++ 11 的...