[leetCode] 68. Text Justification Java

题目:

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.

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

代码:

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> res = new ArrayList<>();
        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) > maxWidth){        //超过了单行最多数
                int space = 0;      //平均的空格数
                int extraSpace = 0;      //多余的空格数
                if(i-last-1 >0){
                    space = (maxWidth - count) / (i-last - 1);      //平均的空格数
                    extraSpace = (maxWidth - count) % (i-last-1);      //多余的空格数}
                }

                StringBuilder stringBuilder = new StringBuilder();
                for(int j=last;j<i;j++){
                    stringBuilder.append(words[j]);
                    if(j < i-1){        //words[i-1]的话后面就不用填空格了,所以这里j<i-1
                        for(int m=0;m<space;m++){
                            stringBuilder.append(" ");
                        }
                        if(extraSpace >0)
                            stringBuilder.append(" ");
                        extraSpace--;
                    }
                }

                //当一行只有一个单词,且没有填满时
                for(int j=stringBuilder.length();j<maxWidth;j++)
                    stringBuilder.append(" ");

                res.add(stringBuilder.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()<maxWidth)
                str.append(" ");
        }
        for(int i=str.length();i<maxWidth;i++)
            str.append(" ");

        res.add(str.toString());
        return res;
    }
}

 

  

 

posted @ 2018-01-08 14:59  荒野第一快递员  阅读(411)  评论(0编辑  收藏  举报