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. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...

  2. python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)

    ### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2)   #(上限,下限,步长)  可以实现对元素或者下标的 ...

  3. NOI题库

    07:机器翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个 ...

  4. 【uoj150】 NOIP2015—运输计划

    http://uoj.ac/problem/150 (题目链接) 题意 给出一棵树以及m个询问,可以将树上一条边的权值修改为0,求经过这样的修改之后最长的边最短是多少. Solution 老早就听说过 ...

  5. bzoj3037 创世纪

    两种解法: 一.树状DP /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring&g ...

  6. 使用Java的嵌套循环打印出平行四边形、等腰三角形、棱形、矩形的星星图案(Java工程师面试必备)

    第一遍是看了视频,听老师讲解嵌套循环的使用,然后到星星图形这一步,当时都觉得听明白了,但是自己去做,就是写不出来 第二遍看了赵老师的教程,看了好熟悉的感觉,还是自己写不出来 第三遍找网上关于图形的嵌套 ...

  7. H2嵌入式数据库

    一 H2 数据库 官网地址. http://www.h2database.com/html/cheatSheet.html

  8. memcached 缓存服务器

    Memcached 缓存服务器 Memcached 是高性能的分布式内存缓存服务器. 一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态web应用的速度.提高可扩展性. 主要特点 ...

  9. Yii2事件

    namespace app\components; use yii\base\Component; use yii\base\Event; class MessageEvent extends Eve ...

  10. phpcms 调取全站文章

    路径:phpcms/module/content/classes/content_tag.class.php 添加如下方法 /** * 列表页标签:主要返回的是主表中数据与附表中数据 * @param ...