Text Justification,文本对齐
问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐。
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.
Return the formatted lines as:
[
"This is an",
"example of text",
"justification. "
]
public class TextJustification {
public ArrayList<String> fullJustify(String[] words, int L)
{
ArrayList<String> res = new ArrayList<String>();
if(words==null || words.length==0)
return res;
int count = 0;//当前行字符串长度和
int last = 0;//每一行头元素下标
for(int i=0;i<words.length;i++)
{
//count是上一次计算的单词的长度,words[i].length()是当前尝试放的一个单词的长度,
//假设当前放上了这个单词,那么这一行单词跟单词间的间隔数就是i-last
//判断这些总的长度加起来是不是大于L(超行数了)
if(count + words[i].length() + (i-last) > L)
{
int spaceNum = 0;
int extraNum = 0;
//因为尝试的words[i]失败了,所以间隔数减1.此时判断剩余的间隔数是否大于0
if( i-last-1 >0)
{
//是间隔的倍数(为啥要减1,因为尝试当前words[i]后发现比L长了,
//所以当前这个单词不能算作这行,所以间隔就减少一个
spaceNum = (L-count)/(i-last-1);
extraNum = (L-count)%(i-last-1);//不是倍数的话还要计算
}
StringBuilder str = new StringBuilder();
for(int j=last;j<i;j++)
{
str.append(words[j]);
if(j<i-1)
{//words[i-1]的话后面就不用填空格了,所以这里j<i-1
for(int k=0;k<spaceNum;k++)
str.append(" ");
if(extraNum>0)
{
str.append(" ");
extraNum--;
}
}
}
//下面这个for循环作用于一行只有一个单词还没填满一行的情况
for(int j=str.length();j<L;j++)
str.append(" ");
res.add(str.toString());
count=0;
last=i;//下一个开始的单词
}//if
count += words[i].length();
}//for
//处理最后一行
StringBuilder str = new StringBuilder();
for(int i=last;i<words.length;i++)
{
str.append(words[i]);
if(str.length()<L)
str.append(" ");
}
for(int i=str.length();i<L;i++)
str.append(" ");
res.add(str.toString());
return res;
}
public static void main(String[] args) {
String[] words = {"This", "is", "an", "example", "of", "text", "justification."};
int l = 16;
TextJustification tj = new TextJustification();
ArrayList<String> list = tj.fullJustify(words, l);
for (String string : list) {
System.out.println(string);
}
}
}
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] 68. 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 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- [Swift]LeetCode68. 文本左右对齐 | Text Justification
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- [MIT6.006] 20. Daynamic Programming II: Text Justification, Blackjack 动态规划II:文本对齐,黑杰克
这节课通过讲解动态规划在文本对齐(Text Justification)和黑杰克(Blackjack)上的求解过程,来帮助我们理解动态规划的通用求解的五个步骤: 动态规划求解的五个"简单&q ...
- 68. Text Justification一行单词 两端对齐
[抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- 微软Build 2017开发者大会午夜趴
时间:2017年5月10号半夜 地点:微软中关村会议室 一年一度的Build大会,微软今年特地组织了一波粉丝到“现场”远程观摩keynote直播,同时在新浪直播间里也有相应的专家进行同步翻译和讲(tu ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Oracle数据库模型(OLAP/OLTP)
数据库模型 选择数据库模型: 联机事务处理OLTP(on-line transaction processing) OLTP是传统的关系数据库的主要应用,基本的.日常的事务处理.例如银行交易. OLT ...
- kafka_2.11-0.10.1.1集群搭建安装配置
在搭建kafka集群之前,请保证zookeeper已安装. 1.下载 官网下载链接:http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/0.10.1.1/ ...
- 微软企业库验证 Validations
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- netty + Protobuf (整合二)
[正文]Protobuf 消息设计 疯狂创客圈 死磕Netty 系列之12 [博客园 总入口 ] 本文说明 本篇是 netty+Protobuf 实战的第二篇,完成一个 基于Netty + Proto ...
- <2014 04 29> c/c++常用库总结
C 标准库 ============================================================================================== ...
- Java程序员面试题集(1-50
下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...
- 一.MySQL入门基础
1.关于cmd界面无法启动mysql: 1)必须要使用管理员身份运行cmd程序 2)如果下载MySQL5.7版本的,在windows服务上Mysql的名字默认是MySQL57,因此在cmd运行 net ...
- 【我的Android进阶之旅】Realm数据库学习资料汇总(持续更新)
介绍 realm是一个跨平台移动数据库引擎,支持iOS.OS X(Objective-C和Swift)以及Android. 2014年7月发布.由YCombinator孵化的创业团队历时几年打造,是第 ...