Leetcode 68. Text Justification 文本调整 解题报告

来源:互联网 发布:淘宝网购物女装15岁 编辑:程序博客网 时间:2024/06/17 09:11

1 解题思想

这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子。
要求有:
1、每一行的字符串长度不能超过一个固定长度maxWidth
2、每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少。并且,空格多的地方只比右边少的多一个
3、最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好

提前贴个解释:
* 这道题关键在于仔细的处理每一个步骤:
* 1、每一行选择K的单词,K个单词的长度+K-1个空格的长度必须要小于maxWidth,这里每次选择满足这个条件的最大值就可以
* 2、对于已经选定了K个单词,首先计算基本空格,也就是space=(maxWidth-所有单词的长度)/ (k-1),但是还有多余出一部分空格,那么就在附加空格的时候,从左边开始每次多加一个,满足题目的左边的空格大于等于右边的(至多多一个)
* 3、注意只有1个单词的场景
* 4、最后一行需要调整,最后一行单词之间的空格只有1个,末尾再用空格补足长度“

2 原题

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. ”
]

3 AC解

public class Solution {    /**     * 这道题关键在于仔细的处理每一个步骤:     * 1、每一行选择K的单词,K个单词的长度+K-1个空格的长度必须要小于maxWidth,这里每次选择满足这个条件的最大值就可以     * 2、对于已经选定了K个单词,首先计算基本空格,也就是space=(maxWidth-所有单词的长度)/ (k-1),但是还有多余出一部分空格,那么就在附加空格的时候,从左边开始每次多加一个,满足题目的左边的空格大于等于右边的(至多多一个)     * 3、注意只有1个单词的场景     * 4、最后一行需要调整,最后一行单词之间的空格只有1个,末尾再用空格补足长度“     * */    public List<String> fullJustify(String[] words, int maxWidth) {        List<String> result = new ArrayList<String>();        int start=0,end=1,n=words.length;        while(start<n){            int compulsorySpaces=0; //必须的空格,为当前选中单词数量-1            int wordLength=words[start].length();//当前单词的数量            while(end<n && compulsorySpaces+1+wordLength+words[end].length()<=maxWidth){ //试探选择最大的单词数量                compulsorySpaces++;                wordLength+=words[end].length();                end++;            }            if(end==n){ //末行特殊处理                StringBuilder sb=new StringBuilder(words[start]);                for(int k=start+1;k<end;k++) sb.append(" "+words[k]);                for(int k=wordLength+compulsorySpaces;k<maxWidth;k++) sb.append(" ");                result.add(sb.toString());                break;            }            if(end-start==1){ //只选中的一个的特殊处理,因为计算空格未出现除数为0的状况                StringBuilder sb=new StringBuilder(words[start]);                for(int k=wordLength;k<maxWidth;k++)                    sb.append(" ");                result.add(sb.toString());            } else{//处理多个空格                int space = (maxWidth-wordLength)/(end-start-1); //基本的空格                int remains = maxWidth-wordLength-(end-start-1)*space; //因为整除未能分配的空格数量                StringBuilder sb=new StringBuilder(words[start]);                for(int k=start+1;k<end;k++){                    for(int l=0;l<space;l++) sb.append(" ");                    if(remains-->0) sb.append(" "); //在大于0,也就是还需要在左边多加空格的时候,多给一个                    sb.append(words[k]);                }                result.add(sb.toString());            }            start=end;            end=end+1;        }        return result;    }}
0 0