Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' '
when necessary so that each line has exactly maxWidthcharacters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
words
contains at least one word.
Example 1:
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.
Example 3:
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int length = 0;
int lastBegin=0;
int lastEnd = 0;
for (int index = 0; index < words.length; index++) {
int tmp = length + words[index].length();
if (tmp >= maxWidth) {
if (tmp == maxWidth) {
lastEnd = index;
} else {
lastEnd = index - 1;
}
addList(res, words, lastBegin, lastEnd, maxWidth);
length = 0;
if (tmp != maxWidth) {
lastBegin = index;
length += (words[index].length() + (index == words.length - 1 ? 0 : 1));
} else {
lastBegin = index + 1;
}
lastEnd = lastBegin;
} else {
length += (words[index].length() + (index == words.length - 1 ? 0 : 1));
if(index == words.length-1) {
lastEnd = index;
addList(res, words, lastBegin, lastEnd, maxWidth);
length = 0;
}
}
}
if(length!=0) {
addList(res, words, lastBegin, lastEnd, maxWidth);
}
return res;
}
public void addList(List<String> res, String[] words, int beginIndex, int endIndex, int maxWidth) {
StringBuffer tmp = new StringBuffer();
// 左对齐
if (endIndex == words.length - 1 || endIndex == beginIndex) {
for (int index = beginIndex; index <= endIndex; index++) {
tmp.append(words[index]).append(index == endIndex ? "" : " ");
}
while (tmp.length() < maxWidth) {
tmp.append(" ");
}
res.add(tmp.toString());
} else {
// 中间对齐
for (int index = beginIndex; index <= endIndex; index++) {
tmp.append(words[index]).append(index == endIndex ? "" : " ");
}
int length = tmp.length();
// 每块可放空格位置的个数 原先就有一块
int per_num_space = (maxWidth - length) / (endIndex - beginIndex) +1;
//无法平分的空格数 表示每块位置先放x个 然后多余的在从第一块开始往后每一块区域加1
int mod_space = (maxWidth-length) % (endIndex - beginIndex);
int[] space_list = new int[endIndex-beginIndex+1];
for(int index=0;index<endIndex - beginIndex;index++) {
space_list[index] = per_num_space;
if(index<=(mod_space-1)) space_list[index] = space_list[index]+1;
}
tmp = new StringBuffer();
for (int index = beginIndex; index <= endIndex; index++) {
tmp.append(words[index]);
for(int i=0;i<space_list[index-beginIndex];i++) {
tmp.append(" ");
}
}
res.add(tmp.toString());
}
}
本题主要是中间对齐存在问题:对每个可防止空格的区域进行计算空格个数,首先是平均个数 然后剩下的空格余数,对其进行前面几个空格位置 平摊处理。