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 ...
随机推荐
- XMLHttpRequest 对象 status 和statusText 属性对照表
XMLHttpRequest 对象 status 和statusText 属性对照表 status statusText 说明 0** - 未被始化 1** - 请求收到,继续处理 100 Conti ...
- java如何计算两个经纬度之间的距离?
/*计算两个经纬度之间的距离 结果单位:米 */public static double getDistance(String lat1Str, String lng1Str, String lat2 ...
- tomcat的虚拟目录映射常用的几种方式
我们在项目部署的时候,可以采用多种方式,接下来我们将在实际中比较常用的几种方式总结如下. 1.可以直接将我们的项目丢到tomcat的webapps目录下,这样当tomcat重启的时候,我们就可以访 ...
- 区块链 block chain 去信任
去中心化:不以参与交易的任何一方为中心 去信任:假定参与交易的任何一方都是不可信任的 区块链受到关注的原因 去中心化.去信任化.智能合约等,正好满足未来互联网持续发展所要求的信息的盖度自动化和高度程序 ...
- DOM 编程入门
DOM (Document Object Model) 文档对象模型 文档: 标记型文档(具有标签, 属性以及标签中封装的数据) 对象: 封装了属性和行为的实例, 可以被直接调用 模型: 所有标记型文 ...
- django-应用中和amdin使用富文本编辑器kindeditor
文章描述.新闻详情和产品介绍等,都需要大量的文字描述信息或图片.视频.文字的编辑等,这个时候我们就需要介绍第三方富文本编辑器. 今天介绍的是django中绑定和应用kindeditor编辑器: 效果如 ...
- 自定义ActionBar图标
<style name="Theme.glTheme" parent="android:Theme.Holo"> <item name=&qu ...
- Python高级教程-高阶函数
Higher-order function(高阶函数) 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-10) 10 但 ...
- kotlin写的几个小例子
Kotlin-AdapterDemo kotlin语言下BaseAdapter,ArrayAdapter,SimpleAdapter,SimpleCursorAdapter四种适配器的示例 工具and ...
- redis的ruby客户端(三)
1. 介绍 clients这里列出了redis所支持的语言的所有客户端程序,其中就有ruby的.有这么多的客户端,说明要实现redis的客户端是不难的.其实你只要掌握一种语言的socket编程就可以实 ...