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 ...
随机推荐
- 别人的Linux私房菜(14)Linux账号管理和ACL权限设置
用户标识符UID.GID 用户的账号信息,主要是指UID对应.组和GID对应 检查系统中是否存在用户bin:id bin 登录shell验证账号密码的步骤:找到/etc/passwd核对是否存在账号, ...
- js怎么把数字转化为字母(A,B.....AA,AB,..)
function createCellPos( n ){ var ordA = 'A'.charCodeAt(0); var ordZ = 'Z'.charCodeAt(0); var len = o ...
- Buffer.h
#ifndef __NOXIMBUFFER_H__ #define __NOXIMBUFFER_H__ #include <cassert> #include <queue> ...
- idea取消double shift全局搜索
一是因为它和输入法经常冲突,二来是因为它完全可以被其它的快捷键代替,如Ctrl+n,ctrl+shift+n,ctrl+shift+f之类的: 取消的方法,按ctrl+shift+a,然后输入regi ...
- Day06 (黑客成长日记) 初识函数和返回值的作用
定义函数: 1.初识函数: 我们在学习字符串时,有这样的操作: li = 'tsy be ba bvake ' print(len(li)) 这样可以打印出li的长度,我们利用了python中的len ...
- shell编写自动化安装dhcp服务
#!/bin/bash#Auth:Darius#自动化安装dhcp服务#"$1"为测试IP,用来查看IP段是否能通eno=`ifconfig|awk '{print $1}'|he ...
- 在windows下golang安装zmq3小记
备忘这个 安装 TDM-GCChttp://tdm-gcc.tdragon.net/download 设置环境变量set C_INCLUDE_PATH=d:\DevTools\Zmq\ZeroMQ 3 ...
- rails应用的部署
简单部署 RAILS_ENV=production rake secret /etc/profile export SECRET_KEY_BASE=刚才生成的密钥 source /etc/profil ...
- java(三)数据库部分
3.1.1.数据库的分类及常用的数据库 数据库分为:关系型数据库和非关系型数据库 关系型:mysql oracle sqlserver等 非关系型:redis,memcache,mogodb,hado ...
- 1-C++的并发世界
1.1 何谓并发 并发的两种方式 多核机器上的真正并行 单核机器的任务切换 并发的两种途径 多进程并发 1.1 多进程并发需要通过操作系统进行进程间通信 多线程并发 2.1 多线程并发需要共享内存 1 ...