05 August 2008
字符串大模拟,分情况讨论:
1
2
3
如果当前行只有一个单词,特殊处理为左对齐;
如果当前行为最后一行,特殊处理为左对齐;
其余为一般情况,分别计算「当前行单词总长度」、「当前行空格总长度」和「往下取整后的单位空格长度」,然后依次进行拼接。当空格无法均分时,每次往靠左的间隙多添加一个空格,直到剩余的空格能够被后面的间隙所均分。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> result = new ArrayList<>();
int len = words.length;
List<String> wordsList = new ArrayList<>();
for (int i = 0; i < len; ) {
// list 装载当前行的所有 word
wordsList.clear();
wordsList.add(words[i]);
int curWord = words[i++].length();
while (i < len && curWord + 1 + words[i].length() <= maxWidth) {
curWord += 1 + words[i].length();
wordsList.add(words[i++]);
}
// corner case 1: 当前行为最后一行,特殊处理为左对齐
if (i == len) {
StringBuilder sb = new StringBuilder(wordsList.get(0));
for (int k = 1; k < wordsList.size(); k++) {
sb.append(" ").append(wordsList.get(k));
}
while (sb.length() < maxWidth) sb.append(" ");
result.add(sb.toString());
break;
}
// corner case 2: 如果当前行只有一个 word,特殊处理为左对齐
int wordsCount = wordsList.size();
if (wordsCount == 1) {
StringBuilder str = new StringBuilder(wordsList.get(0));
while (str.length() != maxWidth) str.append(" ");
result.add(str.toString());
continue;
}
/**
* 其余为一般情况
* wordWidth : 当前行单词总长度;
* spaceWidth : 当前行空格总长度;
* spaceItem : 往下取整后的单位空格长度
*/
int wordWidth = curWord - (wordsCount - 1);
int spaceWidth = maxWidth - wordWidth;
int spaceItemWidth = spaceWidth / (wordsCount - 1);
StringBuilder spaceItem = new StringBuilder();
// 连接一行中的单词
spaceItem.append(" ".repeat(Math.max(0, spaceItemWidth)));
StringBuilder sb = new StringBuilder();
for (int k = 0, sum = 0; k < wordsCount; k++) {
String item = wordsList.get(k);
sb.append(item);
if (k == wordsCount - 1) {
break;
}
sb.append(spaceItem);
sum += spaceItemWidth;
// 剩余的间隙数量(可填入空格的次数)
int remain = wordsCount - k - 1 - 1;
// 剩余间隙数量 * 最小单位空格长度 + 当前空格长度 < 单词总长度,则在当前间隙多补充一个空格
if (remain * spaceItemWidth + sum < spaceWidth) {
sb.append(" ");
sum++;
}
}
result.add(sb.toString());
}
return result;
}
}