-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullJustify.java
More file actions
37 lines (31 loc) · 948 Bytes
/
FullJustify.java
File metadata and controls
37 lines (31 loc) · 948 Bytes
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 68 文本左右对齐
*/
public class FullJustify {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int left=0, right=0;
while (right<words.length){
left = right;
int size = 0;
while (right<words.length){
if(size+words[right].length()+right-left>maxWidth){
break;
}
size += words[right].length();
right++;
}
int space = 0;
if(right-left>1){
space = (maxWidth-size)/(right-left-1);
}
addWords(left, right-1, space, maxWidth, words, res);
}
return res;
}
void addWords(int left, int right, int space, int maxWidth, String[] words, List<String> res){
}
}