算法第八周作业01

Description

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.

Solution

  • 迭代数组的每个字符串
  • 利用计数器记录当前字符串长度和并与maxWidth比较
  • 区分对待最后一行、一行中只有一个字符串、空数组输入等情况
  • 对于间隔数不能整除maxWidth的情况下,前面的间隔数比后面的间隔数大1

Code

public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<>();
        int counter = 0;
        int index = 0;
        // 迭代每个字符串
        for (int i = index; i < words.length; i++) {
            // 判断当前字符(包括空格)是否超过maxWidth
            if (counter + words[i].length() + i - index > maxWidth) {
                // 如果当前字符串够了
                result.add(appendStr(words, index, i - index, maxWidth - counter, false));
                // 清空计数
                counter = 0;
                index = i--;
            } else {
                // 计数字符串长度和
                counter += words[i].length();
            }
        }
        if (counter != 0 || result.size() == 0) {
            // 对于空字符串数组输入和最后一行的情况
            result.add(appendStr(words, index, words.length - index, maxWidth - counter, true));
        }
        return result;
    }

    // 拼接一行字符串
    private String appendStr(String[] words, int index, int num, int spaces, boolean last) {
        StringBuilder sb = new StringBuilder(num * 2);
        if (num == 1) {
            // 对于只有一个字符串情况
            return sb.append(words[index]).append(appendSpace(spaces)).toString();
        } else if(last){
            // 对于最后一行
            for (int i = 0; i < num; i++) {
                sb.append(words[index + i]).append(" ");
            }
            return sb.append(appendSpace(spaces - num)).toString();
        }
        // 对于普通情况,需要构建空格串间隔(前面有些间隔的空格串需要+1)
        String spaceStr = appendSpace(spaces / (num - 1));
        for (int i = 0; i < num - 1; i++) {
            sb.append(words[index + i]).append(spaceStr);
            // 未能整除的前面的空格字符串长度要+1
            if (i < spaces % (num - 1))
                sb.append(" ");
        }
        return sb.append(words[index + num - 1]).toString();
    }

    private String appendSpace(int spaces) {
        StringBuilder sb = new StringBuilder(spaces);
        for (int i = 0; i < spaces; i++)
            sb.append(" ");
        return sb.toString();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值