LeetCode - Text Justification

来源:互联网 发布:vue webpack 引入js 编辑:程序博客网 时间:2024/06/11 16:21

https://leetcode.com/problems/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 exactlyL 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."]
L16.

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.
这道题本身思路不难,难的是细节太多,分支太多。

首先,当不是最后一行且本行多于两个字符串时,平均space = (L-len)/count,剩余的空格数rest = (L-len)%count在rest还大于0时依次分给前面的间隔,因为每个间隔最多分到rest中的一个,所以每次分一个,知道rest变为0为止。

然后,当不是最后一行且本行只有一个字符串时,space = L-len, rest = 0.

再然后,当是最后一行且本行不止一个字符串时,space = 1,最后再padding直到到长度L为止。

最后,当是最后一行且最后一行只有一个字符串时,就直接padding到长度L


public class Solution {    public List<String> fullJustify(String[] words, int L) {        List<String> rst = new LinkedList<String>();        if(words== null || words.length==0) return rst;        int i = 0;        while(i<words.length){            StringBuilder sb = new StringBuilder();            sb.append(words[i]);            int len = words[i].length();            int count = 1;            int j = i+1;            while(j<words.length){                if((len+words[j].length()+count)<=L){                    len+=words[j].length();                    j++;                    count++;                }                else break;            }            int space, rest;            if(count==1){                space = L-len;                rest = 0;            }            else{                space = (L-len)/(count-1);                rest = (L-len)%(count-1);            }                        i++;            if(j==words.length){                if((j-i)>0) sb.append(' ');                while(i<j){                    sb.append(words[i]);                    if(i!=(j-1)){                        sb.append(' ');                    }                    i++;                }                int left = L-len-count+1;                while(left>0){                    sb.append(' ');                    left--;                }            }            else{                int t = space;                while(t>0){                    sb.append(' ');                    t--;                }                if(rest>0){                    sb.append(' ');                    rest--;                }                while(i<j){                    sb.append(words[i]);                    if(i!=(j-1)){                        t= space;                        while(t>0){                            sb.append(' ');                            t--;                        }                        if(rest>0){                            sb.append(' ');                            rest--;                        }                    }                    i++;                }            }            rst.add(sb.toString());        }        return rst;    }}


0 0