【LeetCode】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.
【解析】

题意:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐。

思路:没有什么特别算法,就是模拟,主要分情况判断。首先分两大类,末行和非末行;然后末行所有单词间放一个空格,最后面补充空格;非末行再分两类,如果只有一个单词就靠左放,右边补空格;如果有多个单词,即计算有几个间隔num和几个多余的空格extra(除每两个单词间一个空格外多余的),每个间隔再多方extra/num个,前extra%num个间隔再多放个空格。

【Java代码】

public class Solution {
    public List<String> fullJustify(String[] words, int L) {
        List<String> ans = new ArrayList<String>();
        
        int n = words.length;
        int i = 0;
        while (i < n) {
            int len = words[i].length();
            int j = i + 1;
            while (j < n && len + 1 + words[j].length() <= L) {
                len += 1 + words[j].length();
                j++;
            }
            
            String line = words[i];
            if (j == n) { // if this is the last line
                for (int k = i + 1; k < n; k++) {
                    line += " " + words[k];
                }
                while (line.length() < L) {
                	line += " ";
                }
            } else {
                int extraWhite = L - len;
                int whiteNum = j - i - 1;
                
                if (whiteNum == 0) { // if this line has only one word
                	while (line.length() < L) {
                    	line += " ";
                    }
                } else {
	                for (int k = i + 1; k < j; k++) {
	                    line += " ";
	                    for (int p = 0; p < extraWhite/whiteNum; p++) {
	                        line += " ";
	                    }
	                    if (k - i <= extraWhite%whiteNum) {
	                        line += " ";
	                    }
	                    line += words[k];
	                }
                }
            }
            ans.add(line);
            
            i = j;
        }
        
        return ans;
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值