Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters 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 exactlyLcharacters.

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 exceedLin length.

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.

Solution: 最后一行或者只有一个词的行左对齐(单词间只有一个空格,最后补空格),其他行单词间使空格均匀分布。

    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> ans = new ArrayList<>();
        if (words == null || words.length == 0) {
            return ans;
        }
        int i = 0;

        while (i < words.length) {
            //count:该行可以放单词长度+单词间一个空格
            int count = 0, j = i;
            for (; j < words.length; j++) {
                count += words[j].length() + 1;
                //多加一个单词时,减去最后一个单词和2个空格
                if (count - 1 > maxWidth) {
                    count = count - 2 - words[j].length();
                    j--;
                    break;
                }
                //其余情况减去最后空格
                if (count == maxWidth || count - 1 == maxWidth || j == words.length - 1) {
                    count--;
                    break;
                }
            }
            //num_of_space:空格数
            int num_of_space = j - i, remaining = maxWidth - count;
            StringBuilder sb = new StringBuilder();
            //只有一个单词或者最后一行
            if (num_of_space == 0 || j == words.length - 1) {
                for (int k = i; k <= j; k++) {
                    sb.append(words[k]);
                    if (k == j) {
                        break;
                    }
                    sb.append(' ');
                }                
                for (int k = 0; k < remaining; k++) {
                    sb.append(' ');
                }
            } else {
            //extra_space:需要额外加空格数,need_to_add_extra:前几个要加额外空格数
                int extra_space = remaining / num_of_space, need_to_add_extra = remaining % num_of_space, count_add_extra = 0;

                for (int k = i; k <= j; k++) {
                    sb.append(words[k]);
                    if (k == j) {
                        break;
                    }
                    for (int l = 0; l <= extra_space; l++) {
                        sb.append(' ');
                    }
                    if (count_add_extra < need_to_add_extra) {
                        sb.append(' ');
                        count_add_extra++;
                    }
                }
            }

            ans.add(sb.toString());
            i = j + 1;
        }
        return ans;
    }

results matching ""

    No results matching ""