-
Notifications
You must be signed in to change notification settings - Fork 199
/
FullJustify.java
executable file
·264 lines (208 loc) · 7.56 KB
/
FullJustify.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package Algorithms.string;
import java.util.ArrayList;
import java.util.List;
public class FullJustify {
public static void main(String[] strs) {
String[] words = {"Listen","to","many,","speak","to","a","few."};
int L = 6;
fullJustify(words, L);
}
// SOLUTION 1: recursion.
public List<String> fullJustify1(String[] words, int L) {
List<String> ret = new ArrayList<String>();
if (words == null) {
return ret;
}
rec(words, L, 0, ret);
return ret;
}
public static void rec(String[] words, int L, int index, List<String> list) {
int len = words.length;
if (index >= len) {
return;
}
int LenTmp = L;
int end = index;
for (int i = index; i < len && words[i].length() <= L; i++) {
L -= words[i].length();
// the space follow the word.
L--;
end = i;
}
// 最后一个空格收回
L++;
// Count how many words do we have.
int num = end - index + 1;
int extraSpace = 0;
int firstExtra = 0;
// 单词数大于1,才需要分配,否则所有的空格加到最后即可
if (num > 1) {
extraSpace = L / (num - 1);
// 首单词后多跟的空格
firstExtra = L % (num - 1);
}
StringBuilder sb = new StringBuilder();
for (int i = index; i <= end; i++) {
sb.append(words[i]);
// The space following every word.
if (i != end) {
sb.append(' ');
}
// 不是最后一行
if (end != len - 1) {
// The first words.
if (firstExtra > 0) {
sb.append(' ');
firstExtra--;
}
// 最后一个单词后面不需要再加空格
if (i == end) {
break;
}
// 每个单词后的额外空格
int cnt = extraSpace;
while (cnt > 0) {
sb.append(' ');
cnt--;
}
}
}
// 最后一行的尾部的空格
int tailLen = LenTmp - sb.length();
while (tailLen > 0) {
sb.append(' ');
tailLen--;
}
list.add(sb.toString());
rec(words, LenTmp, end + 1, list);
}
// SOLUTION 2: iteration.
public List<String> fullJustify2(String[] words, int L) {
List<String> ret = new ArrayList<String>();
if (words == null) {
return ret;
}
int len = words.length;
int index = 0;
while (index < len) {
int LenTmp = L;
int end = index;
for (int i = index; i < len && words[i].length() <= LenTmp; i++) {
LenTmp -= words[i].length();
// the space follow the word.
LenTmp--;
end = i;
}
// 最后一个空格收回
LenTmp++;
// Count how many words do we have.
int num = end - index + 1;
int extraSpace = 0;
int firstExtra = 0;
// 单词数大于1,才需要分配,否则所有的空格加到最后即可
if (num > 1) {
extraSpace = LenTmp / (num - 1);
// 首单词后多跟的空格
firstExtra = LenTmp % (num - 1);
}
StringBuilder sb = new StringBuilder();
for (int i = index; i <= end; i++) {
sb.append(words[i]);
// The space following every word.
if (i != end) {
sb.append(' ');
}
// 不是最后一行
if (end != len - 1) {
// The first words.
if (firstExtra > 0) {
sb.append(' ');
firstExtra--;
}
// 最后一个单词后面不需要再加空格
if (i == end) {
break;
}
// 每个单词后的额外空格
int cnt = extraSpace;
while (cnt > 0) {
sb.append(' ');
cnt--;
}
}
}
// 最后一行的尾部的空格
int tailLen = L - sb.length();
while (tailLen > 0) {
sb.append(' ');
tailLen--;
}
ret.add(sb.toString());
index = end + 1;
}
return ret;
}
// SOLUTION 3: iteration2
public static List<String> fullJustify(String[] words, int L) {
List<String> ret = new ArrayList<String>();
if (words == null) {
return ret;
}
int len = words.length;
int index = 0;
while (index < len) {
int LenTmp = L;
int end = index;
for (int i = index; i < len && words[i].length() <= LenTmp; i++) {
LenTmp -= words[i].length();
// the space follow the word.
LenTmp--;
end = i;
}
// 最后一个空格收回
LenTmp++;
// Count how many words do we have.
int num = end - index + 1;
int extraSpace = 0;
int firstExtra = 0;
// 单词数大于1,才需要分配,否则所有的空格加到最后即可
if (num > 1) {
extraSpace = LenTmp / (num - 1);
// 首单词后多跟的空格
firstExtra = LenTmp % (num - 1);
}
StringBuilder sb = new StringBuilder();
for (int i = index; i <= end; i++) {
sb.append(words[i]);
int cnt = 0;
if (i == end) {
break;
}
// 不是最后一行
if (end != len - 1) {
// The first words.
if (firstExtra > 0) {
cnt++;
firstExtra--;
}
// 最后一个单词后面不需要再加空格
// 每个单词后的额外空格
cnt += extraSpace;
}
// 1: 每个单词后本来要加的空格
appendSpace(sb, cnt + 1);
}
// 最后一行的尾部的空格,或者是只有一个单词的情况
appendSpace(sb, L - sb.length());
ret.add(sb.toString());
index = end + 1;
}
return ret;
}
public static void appendSpace(StringBuilder sb, int cnt) {
while (cnt > 0) {
sb.append(' ');
cnt--;
}
}
}