算法第八周作业01

来源:互联网 发布:oracle数据库课程设计 编辑:程序博客网 时间:2024/06/01 21:32

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
原创粉丝点击