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.

解题思路:

直接按照约束条件老实实现即可,JAVA实现如下:

     static public List<String> fullJustify(String[] words, int maxWidth) {
List<String> list=new ArrayList<String>();
int start=0,end=-1,sum=0;
for(int i=0;i<words.length;i++){
sum+=words[i].length();
if(sum>maxWidth){
i--;
start=end+1;
end=i;
sum=0;
list.add(oneString(words,start,end,maxWidth));
continue;
}
sum++;
}
if(sum>0)
list.add(oneString(words,end+1,words.length-1,maxWidth));
return list;
}
static public String oneString(String[] words,int start,int end,int maxWidth){
StringBuilder sb=new StringBuilder();
if(start==end)
sb.append(words[start]);
else if(end==words.length-1){
for(int i=start;i<=end-1;i++)
sb.append(words[i]+" ");
sb.append(words[end]);
}
else{
int spaceSum=maxWidth;
for(int i=start;i<=end;i++)
spaceSum-=words[i].length();
int extra=spaceSum-(spaceSum/(end-start))*(end-start);
for(int i=start;i<=end;i++){
sb.append(words[i]);
for(int j=0;j<spaceSum/(end-start)&&i!=end;j++)
sb.append(" ");
if(extra-->0)
sb.append(" ");
}
}
while(sb.length()<maxWidth)
sb.append(" ");
return sb.toString();
}

Java for LeetCode 068 Text Justification的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  2. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  4. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  7. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

  8. Text Justification leetcode java

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  9. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. Teradata 语句简单优化

    Teradata 基本查询语言和SQL server 是一致的.有很小的区别. 功能没有SQL 全面,界面没有SQL 好看~ 1. teradata 里面经常会报一种错误: no enough spo ...

  2. 积木(DP)问题

    问题:Do you remember our children time? When we are children, we are interesting in almost everything ...

  3. Request.InputStream 接收Post Data

    今天 用 Request.Form /Request.Params 等怎么也获取不到客户发过来的Post 数据 后来试着用了 Request.InputStream 竟然可以 using (Syste ...

  4. sprintf

    功能:将数据格式化到字符串中 原型:int sprintf( char *buffer, const char *format, [ argument] … );返回值是这个字符串的长度 上次我企图这 ...

  5. ECSHOP后台商品列表显示商品缩略图

    ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06   ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...

  6. 严格遵守“第一级DOM”能够让你避免与兼容性有关的任何问题

    1级DOM:1级DOM在1998年10月份成为W3C的提议,由DOM核心与DOM HTML两个模块组成.DOM核心能映射以XML为基础的文档结构,允许获取和操作文档的任意部分.DOM HTML通过添加 ...

  7. 锋利的jQuery-1--end()

    1.导航菜单:选中后显示当前标签下的子标签并且高亮,并且隐藏其他同级标签,(一行级联操作即完成),主要看end()函数用法. end(): 回到最近的一个"破坏性"操作之前.就是指 ...

  8. python之字符聊天小工具

    server side: # coding: gb2312#socket server端#获取socket构造及常量from socket import *#''代表服务器为localhostmyHo ...

  9. Linux下/proc目录简介

    文章转载至:http://blog.csdn.net/zdwzzu2006/article/details/7747977 1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在 ...

  10. SQL 两种表复制语句

    1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...