LeetCode - Text Justification

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值