题目

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.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

题解:

下面讲解引用自Code Ganker(http://blog.csdn.net/linhuanmars/article/details/24063271),代码部分我用注释都解释了下。

“这
道题属于纯粹的字符串操作,要把一串单词安排成多行限定长度的字符串。主要难点在于空格的安排,首先每个单词之间必须有空格隔开,而当当前行放不下更多的
单词并且字符又不能填满长度L时,我们要把空格均匀的填充在单词之间。如果剩余的空格量刚好是间隔倍数那么就均匀分配即可,否则还必须把多的一个空格放到
前面的间隔里面。实现中我们维护一个count计数记录当前长度,超过之后我们计算共同的空格量以及多出一个的空格量,然后将当行字符串构造出来。最后一
个细节就是最后一行不需要均匀分配空格,句尾留空就可以,所以要单独处理一下。时间上我们需要扫描单词一遍,然后在找到行尾的时候在扫描一遍当前行的单
词,不过总体每个单词不会被访问超过两遍,所以总体时间复杂度是O(n)。而空间复杂度则是结果的大小(跟单词数量和长度有关,不能准确定义,如果知道最
后行数r,则是O(r*L))。代码如下:”

代码如下:

 1 public ArrayList<String> fullJustify(String[] words, int L) {
 2     ArrayList<String> res = new ArrayList<String>();
 3     if(words==null || words.length==0)
 4         return res;
 5     int count = 0;
 6     int last = 0;
 7     for(int i=0;i<words.length;i++){
 8         //count是上一次计算的单词的长度,words[i].length()是当前尝试放的一个单词的长度,
 9         //假设当前放上了这个单词,那么这一行单词跟单词间的间隔数就是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;//下一个开始的单词
         }
         count += words[i].length();
     }
     
     //处理最后一行
     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;
 }

Reference:

http://blog.csdn.net/linhuanmars/article/details/24063271

Text Justification leetcode java的更多相关文章

  1. Text Justification [LeetCode]

    Problem Description:http://oj.leetcode.com/problems/text-justification/ Note: Just be careful about ...

  2. Java for LeetCode 068 Text Justification

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

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

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

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

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

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

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

  6. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  8. LeetCode: Text Justification 解题报告

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

  9. LeetCode OJ——Text Justification

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

随机推荐

  1. 【BZOJ 4818】 4818: [Sdoi2017]序列计数 (矩阵乘法、容斥计数)

    4818: [Sdoi2017]序列计数 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 560  Solved: 359 Description Al ...

  2. android 四大组件

     韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 活动,服务,广播接受者,内容提供者. 活动 能够提供 用户界面.服务 没有用户界面.广 ...

  3. bzoj3111: [Zjoi2013]蚂蚁寻路

    题目链接 bzoj3111: [Zjoi2013]蚂蚁寻路 题解 发现走出来的图是一向上的凸起锯齿状 对于每个突出的矩形dp一下就好了 代码 /* */ #include<cstdio> ...

  4. BZOJ2330 SCOI2011糖果

    复习了一波差分约束. http://blog.csdn.net/my_sunshine26/article/details/72849441 构图方式记住就好. 本题要倒序插入否则会被卡. #incl ...

  5. group by 字符串合并 有关问题

    group by 字符串合并 有关问题 group by 字符串合并 问题 如下表: TYPE NAME C123 张三 C189 李四 C123 王一 C123 丁丁 C189 刘某 查询出如下形式 ...

  6. RabbitMQ简单使用

    环境搭建: RabitMQ是用Elang编写的,虽然Elang本身是跨平台的,但也同时意味着搭建Rabit环境需要首先配置Elang环境.配置RabitMQ的网上教程还比较多的: windows 下 ...

  7. Redis使用小结

    Redis官方没有windows版本的,对于Windows环境的redis,有如下两个方案 微软的移植版本,但只支持到3.2 下载地址 win10及以上的版本直接通过win10的liunx子系统执行 ...

  8. Technical Information ARM-related JTAG / SWD / SWV / ETM Target Interfaces

    https://www.computex.co.jp/eg/products/pdf/technical_pdf/arm_if01_gijutsu_eng.pdf

  9. [廖雪峰] Git 分支管理(1):创建与合并分支(HEAD、master、dev、指针)

    每次提交,Git 都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支.HEAD 严格来说不是指向提交,而是指向 mas ...

  10. OOP设计模式[JAVA]——04命令模式

    命令模式 命令模式的意图 命令模式属于对象的行为模式.别名又叫:Action或Transaction. 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求 ...