forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
50 lines (48 loc) · 1.7 KB
/
Solution.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
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> ans = new ArrayList<>();
int n = words.length;
for (int i = 0; i < n;) {
List<String> t = new ArrayList<>();
int cnt = words[i].length();
t.add(words[i++]);
while (i < n && cnt + 1 + words[i].length() <= maxWidth) {
cnt += 1 + words[i].length();
t.add(words[i++]);
}
if (i == n || t.size() == 1) {
// this is the last line or only one word in a line
String left = String.join(" ", t);
String right = " ".repeat(maxWidth - left.length());
ans.add(left + right);
if (i == n) {
break;
}
continue;
}
int wordsWidth = cnt - t.size() + 1;
int spaceWidth = maxWidth - wordsWidth;
List<String> spaces = partition(spaceWidth, t.size() - 1);
StringBuilder sb = new StringBuilder(t.get(0));
for (int j = 0; j < t.size() - 1; ++j) {
sb.append(spaces.get(j));
sb.append(t.get(j + 1));
}
ans.add(sb.toString());
}
return ans;
}
private List<String> partition(int n, int cnt) {
List<String> ans = new ArrayList<>();
int base = n / cnt;
int mod = n % cnt;
for (int i = 0, j = 0; i < cnt; ++i, ++j) {
StringBuilder sb = new StringBuilder(" ".repeat(base));
if (j < mod) {
sb.append(' ');
}
ans.add(sb.toString());
}
return ans;
}
}