forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_68.java
61 lines (57 loc) · 2.06 KB
/
_68.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _68 {
public static class Solution1 {
public List<String> fullJustify(String[] words, int L) {
ArrayList<String> result = new ArrayList();
if (words == null || words.length == 0) {
return result;
}
int count = 0;
int last = 0;
for (int i = 0; i < words.length; i++) {
if (count + words[i].length() + (i - last) > L) {
int spaceNum = 0;
int extraNum = 0;
if (i - last - 1 > 0) {
spaceNum = (L - count) / (i - last - 1);
extraNum = (L - count) % (i - last - 1);
}
StringBuilder sb = new StringBuilder();
for (int j = last; j < i; j++) {
sb.append(words[j]);
if (j < i - 1) {
for (int k = 0; k < spaceNum; k++) {
sb.append(" ");
}
if (extraNum > 0) {
sb.append(" ");
}
extraNum--;
}
}
for (int j = sb.length(); j < L; j++) {
sb.append(" ");
}
result.add(sb.toString());
count = 0;
last = i;
}
count += words[i].length();
}
StringBuilder sb = new StringBuilder();
for (int i = last; i < words.length; i++) {
sb.append(words[i]);
if (sb.length() < L) {
sb.append(" ");
}
}
for (int i = sb.length(); i < L; i++) {
sb.append(" ");
}
result.add(sb.toString());
return result;
}
}
}