LeetCode 84 Text Justification

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.

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.
分析:

先翻译一下题目好了:

把一个字符串数组里的数组按顺序放在一个矩阵里,矩阵每行宽度为L

1, 希望每行单词尽量平均分布,就是说,剩余的空格应尽量平均分配在单词中间,不能平均分配的零头空格,从左到右分配;

2,如果某行只有一个单词,则空格补在后面;

3,最后一行,单词之间只要一个空格,其他空格补在后面。

大概就是这个意思了,条件很琐碎。

记录每一行开头的单词(下标)last,每行维护一个count记录当前字符数,

count + words[i] + i-last > L 说明加上words[i]之后及其所需的空格之后,超过L,那么words[i] 不应该放在当前行;

spaceNum 记录两个单词之间的空格数, spaceNum = (L - count)/(i-last-1), 其中i-last-1是单词间隙数;

extraNum记录不能平均分配的零头空格数,extraNum = (L-count)%(i-last-1), 这些零头从左到右分配。

以上是基本思路,特殊处理的地方就是:

1,每行只有一个单词的情况;

2,最后一行。

public class Solution {
    public List<String> fullJustify(String[] words, int L) {
        
        List<String> res = new ArrayList<String>();
        if(words == null || words.length == 0)
            return res;
        int count = 0;//记录当前行长度
        int last = 0;//记录当前行第一个单词下标
        for(int i=0; i<words.length; i++){
            //i-last是如果加上words[i]所需的空格数
            if(count + words[i].length() + (i-last) > L){//空间不够,words[i]应该放在下一行
                int spaceNum = 0;
                int extraNum = 0;
                if(i-last-1 > 0){//单词间隔数
                    spaceNum = (L-count)/(i-last-1);//平均两个单词之间的空格数
                    extraNum = (L-count)%(i-last-1);//剩下的无法平均的零头
                }
                StringBuffer sb = new StringBuffer();
                for(int j=last; j<i; j++){
                    sb.append(words[j]);
                    if(j < i-1){//如果不是此行最后一个单词,则后面应该加空格
                        for(int k=0; k<spaceNum; k++)
                            sb.append(" ");
                        //零头空格从左到右每个地方分配一个,直到没有
                        if(extraNum > 0){
                            sb.append(" ");
                            extraNum--;
                        }
                    }
                }
                //处理一行只有一个单词的边界情况,如果是这样,单词左对齐,右边补空格
                for(int j=sb.length(); j<L; j++)
                    sb.append(" ");
                //此行处理结束
                res.add(sb.toString());
                count = 0;//当前字符数清0;
                last = i; //此行未处理的最后一个单词作为下一行的开头
            }
            count += words[i].length();
        }
        //处理最后一行
        StringBuffer sb = new StringBuffer();
        for(int i=last; i<words.length; i++){
            sb.append(words[i]);
            if(sb.length()<L)
                sb.append(" ");
        }
        //后面补零
        for(int i=sb.length(); i<L; i++)
            sb.append(" ");
        //最后一行加入结果集
        res.add(sb.toString());
        
        return res;
    }
}


你可能感兴趣的:(LeetCode,text,Justification)