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

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
             because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

文本左右对齐。这道题没有涉及算法概念,不过很考验编程功底。

这道题就是实现word里的左右对齐功能,需要注意如下几点。

  • 每行尽可能多放单词
  • 单词之间的距离至少是一个空格
  • 如果有多余的空格但是不足以再多放一个单词的话,把空格均匀分配到每两个单词中间
  • 最后一行需要左对齐,用空格补足后面

其余的请参见注释。

时间O(n)

空间O(n) - StringBuilder

Java实现

 1 class Solution {
 2     public List<String> fullJustify(String[] words, int maxWidth) {
 3         // 记录结果
 4         List<String> res = new ArrayList<>();
 5         int n = words.length;
 6         int index = 0;
 7         while (index < n) {
 8             // totalChars - 当前行上的字母数量
 9             int totalChars = words[index].length();
10             // 下一个单词
11             int last = index + 1;
12             while (last < n) {
13                 // 试着多加一个单词 看当前行是否能塞得下 不行就break
14                 if (totalChars + 1 + words[last].length() > maxWidth) {
15                     break;
16                 }
17                 // 计算当前行的字母数
18                 totalChars += 1 + words[last].length();
19                 last++;
20             }
21             // 计算每行有几个gap 此时的last应该是下一行第一个单词的index
22             int gaps = last - index - 1;
23             StringBuilder sb = new StringBuilder();
24             // 如果是最后一行
25             if (last == n || gaps == 0) {
26                 // 加入单词和空格
27                 for (int i = index; i < last; i++) {
28                     sb.append(words[i]);
29                     sb.append(' ');
30                 }
31                 // 去掉最后一个单词之后的空格
32                 // 这一行是有必要的 尤其是如果最后一行存在的单词数量 + 空格正好等于maxWidth的情况
33                 sb.deleteCharAt(sb.length() - 1);
34                 // 之后补足空格
35                 while (sb.length() < maxWidth) {
36                     sb.append(' ');
37                 }
38             } else {
39                 // 开始拼接当前行的单词们
40                 // space for each gap
41                 int spaces = (maxWidth - totalChars) / gaps;
42                 // extra spaces on each line
43                 int rest = (maxWidth - totalChars) % gaps;
44                 for (int i = index; i < last - 1; i++) {
45                     sb.append(words[i]);
46                     sb.append(' ');
47                     for (int j = 0; j < spaces + (i - index < rest ? 1 : 0); j++) {
48                         sb.append(' ');
49                     }
50                 }
51                 sb.append(words[last - 1]);
52             }
53             res.add(sb.toString());
54             index = last;
55         }
56         return res;
57     }
58 }

 

LeetCode 题目总结