算法第八週作業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();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章