Text Justification
2015-07-28 21:26
253 查看
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.
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.
public class Solution { public List<String> fullJustify(String[] words, int maxWidth) { ArrayList<String> result = new ArrayList<String>(); int len=words.length,curLen=0,lastI=0; for(int i=0;i<=len;i++){ if(i==len||curLen+words[i].length()+i-lastI>maxWidth){ StringBuffer buf=new StringBuffer(); int spaceLen=maxWidth-curLen; int spaceSlots=i-lastI-1; if(spaceSlots==0||i==len){ for(int j=lastI;j<i;j++){ buf.append(words[j]); if(j!=i-1) appendSpace(buf, 1); } appendSpace(buf, maxWidth-buf.length()); }else { int spaceEach=spaceLen/spaceSlots; int sapceExtra=spaceLen%spaceSlots; for(int j=lastI;j<i;j++){ buf.append(words[j]); if(j!=i-1) appendSpace(buf, spaceEach+(j-lastI<sapceExtra?1:0)); } } result.add(buf.toString()); lastI=i; curLen=0; } if(i<len) curLen+=words[i].length(); } return result; } private void appendSpace(StringBuffer sb, int count) { for (int i = 0; i < count; i++) sb.append(' '); } }
相关文章推荐
- 斐波那契数列F(n)【n超大时的(矩阵加速运算) 模板】
- Android如何实现侧拉栏
- hdu 5323 Solve this interesting problem(15多校第三场1008)(暴力)
- Android手势滑动识别器应用
- 循环次数
- 暑假集训第三周 STL R - What Are You Talking About
- C#委托
- 程序开发的23种模式
- CALayer 和 CAGradientLayer简单使用
- XHTML基础
- hdu-1509 Windows Message Queue(优先队列)
- 点击事件在XML标签属性中声明
- 一些前端的心得体会,笔记
- win8 x64下安装mysql-workbench 5.2无法运行问题
- leetcode 最长回文串
- 黑马程序员------OC 面向对象和面向过程思想(基础)
- php.ini配置文件
- Spring的Bean的生命周期
- 《Java程序性能优化》学习笔记之ArrayList和LinkedList
- pring @ResponseBody 导致的中文乱码处理