68 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 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.

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.

解题思路:
本题的考点就是字符串操作,思路其实不算复杂,但是要注意的细节有很多。
首先,要找出 maxWidth 范围内能容纳多少个单词,记录下这些单词的下标范围。
其次,将空格均匀地分布到单词与单词之间,如果单词不能均匀分布,就把多出来的空格放到左边的单词间隔中。
最后,将上面形成的字符串放到 list 中。

  • 对于最后一个字符串来说,单词与单词之间只要求有一个空格即可,如果此时字符串长度不足 maxWidth,就在字符串后面添加空格。
  • 对于没有单词的字符串数组来说,如果 maxWidth > 1,要用空格拼凑成一个长度满足 maxWidth 的字符串。

代码实现:

public class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> list = new ArrayList();
        if(words == null || words.length == 0 || (words.length == 1 && words[0].length() == 0)) {
            StringBuilder sb = new StringBuilder();
            while(maxWidth -- > 0)
                sb.append(" ");
            list.add(sb.toString());
            return list;
        }
        int start = 0; // 指示单词下标
        int end = 0;
        while(end < words.length) {
            start = end;
            int len = maxWidth;
            len = len - words[end].length();
            end ++;
            if(end == words.length) { // 只有一个单词
                StringBuilder str = new StringBuilder(words[start]);
                while(str.length() < maxWidth)
                    str.append(" ");
                list.add(str.toString());
                break;
            }
            while(end < words.length && len - 1 - words[end].length() >= 0) { // 至少两个单词
                len = len - words[end].length();
                len --; // 减去words[end] 和 words[end - 1] 之间的空格长度
                end ++;
            }
            if(end == words.length) { // 已遍历完所有words中的单词,将 start 到 end - 1 的单词放入一个字符串中
                StringBuilder str = new StringBuilder(words[start]);
                start ++;
                while(start < end) {
                    str.append(" ");
                    str.append(words[start ++]);
                }
                while(str.length() < maxWidth)
                    str.append(" ");
                list.add(str.toString());
                break;
            }
            int spaceNum = end - start - 1; // 单词间空格的个数
            len += spaceNum; // 在上面 while 中减去了 spaceNum 个空格,将它们加回来,获得合并后字符串所需的全部空格
            StringBuilder space = new StringBuilder();
            for(int i = 0; spaceNum > 0 && i < len / spaceNum; i ++) // 所有单词之间都应该分得的空格数
                space.append(" ");
            StringBuilder str = new StringBuilder(words[start]);
            for(int i = start + 1; i < end; i ++) {
                str.append(space);
                if(i - start - 1 < len % spaceNum) // 如果所有空格不能平均分到每一个单词之间,就将多余的空格分给左边
                    str.append(" ");
                str.append(words[i]);
            }
            while(str.length() < maxWidth)
                str.append(" ");
            list.add(str.toString());
        }
        return list;
    }
}
24 / 24 test cases passed.
Status: Accepted
Runtime: 2 ms
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值