leetcode — text-justification
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* Source : https://oj.leetcode.com/problems/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.
*
*
* 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.
*/
public class TextJustification {
/**
* 格式化给定的单词串,每行长度固定
*
* @param words
* @return
*/
public String[] justify (String[] words, int length) {
if (words.length < 1) {
return words;
}
if (words.length == 1) {
words[0] = words[0] + getSpace(length - words[0].length());
return words;
}
int gap = 0;
int lineLength = words[0].length();
List<String> result = new ArrayList<String>();
List<String> lineWords = new ArrayList<String>(){{add(words[0]);}};
int index = 1;
while (index < words.length) {
int sum = gap + lineLength + words[index].length();
if (sum >= length) {
result.add(buildLine(lineWords, gap, length - gap - lineLength));
gap = -1;
lineLength = 0;
lineWords.clear();
}
gap++;
lineLength += words[index].length();
lineWords.add(words[index]);
index ++;
// 最后一行
if (index == words.length) {
result.add(buildLine(lineWords, gap, length - gap - lineLength));
}
}
return result.toArray(new String[result.size()]);
}
private String buildLine (List<String> words, int gap, int remain) {
if (words.size() == 1) {
return words.get(0) + getSpace(remain);
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.size() - 1; i++) {
int spaceLen = 1 + remain / gap;
if (i < remain % gap) {
spaceLen ++;
}
result.append(words.get(i) + getSpace(spaceLen));
}
result.append(words.get(words.size()-1));
return result.toString();
}
private String getSpace (int n) {
String space = "";
for (int i = 0; i < n; i++) {
space += " ";
}
return space;
}
public static void main(String[] args) {
TextJustification textJustification = new TextJustification();
String[] words = new String[]{"This", "is", "an", "example", "of", "text", "justification."};
System.out.println(Arrays.toString(textJustification.justify(words, 16)));
}
}
leetcode — text-justification的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- LeetCode: Text Justification 解题报告
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- [Leetcode] text justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [LeetCode] Text Justification words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 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 OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- USB设备类学习笔记
usb audio class 版本目前有3个版本 分别是1.0,2.0,3.0:1.0针对各个厂家的设备具有不同的描述符,而2.0则将它们统一简化,3.0则是最新的,还没有与2.0作进一步比较:因 ...
- spring bean 注入
概念 http://developer.51cto.com/art/200610/33311.htm http://kb.cnblogs.com/page/45266/ ==https://www.c ...
- mybatis注解SQL
在网上找了很久,特别是批量插入,很久都没有找到,终于最后一不小心就搞出来了.所以想写个随笔保存下来,一方面想提高自己的总结能力,一方面为了结识有相同兴趣的朋友(第一篇博客我的天纳
- 【转】RPC介绍
转自:http://www.cnblogs.com/Vincentlu/p/4185299.html 摘要: RPC——Remote Procedure Call Protocol,这是广义上的解释, ...
- one-to-one 一对一映射关系(转 wq群)
2.配置对应的xml配置文件 person的配置文件 idCard的配置文件 idCard的配置文件 3.测试 运行测试程序后,控制台输出两条语句
- vue数据双向绑定
Vue的双向绑定是通过数据劫持结合发布-订阅者模式实现的,即通过Object.defineProperty监听各个属性的setter,然后通知订阅者属性发生变化,触发相应的回调. 整个过程分为以下几步 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- 判断exe执行结束,事件监听
[转载]C#调用Exe文件的方法及如何判断程序调用的exe已结束 原文地址:C#调用Exe文件的方法及如何判断程序调用的exe已结束作者:guoguo 很简单的代码就可以实现C#调用EXE文件 ...
- 【.NET Core项目实战-统一认证平台】第三章 网关篇-数据库存储配置(1)
[.NET Core项目实战-统一认证平台]开篇及目录索引 本篇将介绍如何扩展Ocelot中间件实现自定义网关,并使用2种不同数据库来演示Ocelot配置信息存储和动态更新功能,内容也是从实际设计出发 ...
- 王垠的40行代码,究竟diao在哪里
王垠是谁? 不用我说了吧!!! 别傻谈,亮码瞧! ;; A simple CPS transformer which does proper tail-call and does not;; dupl ...