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 ...
随机推荐
- duboo服务调用不到的原因(dubbo启动消费者报错:No provider available for the service)
com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method queryTemplate in the service com.x.a ...
- 《JAVA多线程编程核心技术》 笔记:第四章、Lock的使用
一.使用ReentrantLock类1.1 ReentrantLock的使用:1.2 ReentrantLock的不足:1.3 正确使用Condition实现等待/通知1.4 使用多个Conditio ...
- sql---如何把sql查询出来的结果当做另一个sql的条件查询,1、语句2、with as
'; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多 ...
- Outlook Top of Information Store
Actually I got to thinking this might make a good blog post so I took a closer look - Try this: On t ...
- Chomsky_hierarchy
Grammar Languages Automaton Production rules (constraints) Type-0 Recursively enumerable Turing ma ...
- 关于jQuery中nth-child和nth-of-type的详解
首先贴出来HTML的代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- JDK源代码学习系列05----LinkedList
JDK源代码学习系列05----LinkedList 1.LinkedList简单介绍 LinkedList是基于双向 ...
- 枢轴点(Pivot Point)系统
Pivot Point是日内交易方法,非常简单实用,是一套非常“单纯”的阻力支持体系,大概是10年前一个做期货的高手发明的方法,至今已经广泛的用在股票.期货.国债.指数等高成交量的商品上.经典的Piv ...
- CoreThink开发(十二)更改默认出错异常页防止暴露敏感数据
默认的异常页会打印文件位置,而且是绝对路径,会打印SQL语句,真实上线一定不要用这个默认的,而且关闭trace关闭调试模式也不行. 针对CoreThink1.2 ThinkPHP3.2 这个文件在 A ...
- alter session set events
.alter session set events 一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空 ...