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 ...
随机推荐
- 第七周助教工作总结——NWNU李泓毅
本周应批改作业0,实批改作业0 因本周开始软工团队项目,故本周几位助教正在制定团队项目题目并且处理相关工作. 这一次的作业将于4月17日前进行提交,作业为软件研发团队的组建.
- 前后台分离开发时遇到循环引用问题"$ref"
1. 遇到的问题 { "errMsg": "", "data": { "baseinfo": { "freeT ...
- 《Linux就该这么学》第十七天课程
想学Squid可以前往https://www.linuxprobe.com/chapter-16.html讲的非常细 Squid服务程序提供正向代理服务 Squid服务程序提供的反向代理模式
- vue + spring boot + spring security 前后端分离 携带 Cookie 登录实现 只写了个登录
最近想弄一下vue 所以就自己给自己找坑入 结果弄的满身是伤 哈哈哈 首先我说下 前后端分离 跨域请求 我在网上找了一些 可是都是针对于 spring boot 的 我自己还有 securi ...
- 使用@Autowird注入报空指针异常
new的对象不能调用此对象里面注入的其他类,如果想要调用注入的其他类,则此new的对象要使用@componet将此类注入. 原因:
- 快乐python 零基础也能P图 —— PIL库
Python PIL PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. ...
- 安装memcache遇到的坑
memcached 在make的时候出错,解决办法: # vim memcached.c 修改如下几行56 /* FreeBSD 4.x doesn't have IOV_MAX exposed. * ...
- vue的学习之路
一.vs code中,适合vue的前端插件 查看网址:http://blog.csdn.net/caijunfen/article/details/78749766 二.如何使用git从gitub上拉 ...
- Flask框架(一)
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return '<h1>hello w ...
- 从零起步做到Linux运维经理, 你必须管好的23个细节
“不想成为将军的士兵,不是好士兵”-拿破仑 如何成为运维经理? 一般来说,运维经理大概有两种出身:一种是从底层最基础的维护做起,通过出色的维护工作,让公司领导对这个人非常认可,同时对Linux运维工作 ...