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的更多相关文章

  1. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  2. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  3. LeetCode:Text Justification

    题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...

  4. LeetCode: Text Justification 解题报告

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. [Leetcode] text justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. [LeetCode] Text Justification words显示的排序控制

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  7. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  8. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  10. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. DataTable调整顺序

    DataTable中手动调整列的顺序 DataTable中手动调整列的顺序(列序,reorder,Rearrange)DataTable dt = new DataTable(); dt.Column ...

  2. Fiddler-设置取消自动更新

    fiddler 启动时老弹出要更新,但不想更新,可以这样设置 Tools-Optons->General 把第一个√去掉

  3. CentOS 7 nginx 1.8.1安装

    OS版本:CentOS 7.2nginx版本:1.8.1所需包:openssl-1.0.2m.tar.gz zlib-1.2.8.tar.gz pcre-8.36.tar.gz nginx-1.8.1 ...

  4. Java 输入流(一)ByteArrayInputStream

    概述 ByteArrayInputStream类是从内存中的字节数组中读取数据,因此它的数据源是一个字节数组.

  5. redux之applyMiddleware

    redux之所以伟大就在于中间件了,中间件为redux提供了无限可能.redux中中间件是一个不太容易理解的概念,因为涉及到compose.hoc等函数式的概念,看源代码总是懵懵的感觉.今天我们就来详 ...

  6. android 图形图像

    Canvas 画布 paint 画笔 Path 路径Path代表任意多条直线连接而成的任意图形,当Canvas根据Path绘制时,它可以绘制出任意的形状 使用 Matrix 控制图像或组件变换步骤:① ...

  7. TCP的客户端搭建

    一.设计目标 之前已经写过了TCP服务器原理及通过visual studio 验证 SOCKET编程:搭建一个TCP服务器,这里我们搭建一个TCP客户端界面并对各功能进行实现.设计效果如下: 二.实验 ...

  8. [转] External(and Live) snapshots with libvirt

    http://kashyapc.com/ Raw image is a blob of data exposed directly in VM as block device, it can't sn ...

  9. Javascript高级编程学习笔记(21)—— 对象原型

    JS中对象相关的最重要的恐怕就是原型链了 原型链也是JS中对象继承的实现的基础 接昨天的文章,我们使用构造函数创建对象的时候仍然存在一些问题 那就是所有的实例没法共用一个函数 这样无疑会造成极大的内存 ...

  10. OAuth2.0认证详解

    目录 什么是OAuth协议 OAuth2.0是为了解决什么问题? OAuth2.0成员和授权基本流程 OAuth2.0成员 OAuth2.0基本流程 什么是OAuth协议 OAuth 协议为用户资源的 ...