Description
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 maxWidth characters.
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.
Examples
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 "
]
解题思路
先解读一下题目,题目的意思就是做对齐
对于一行中有多个单词的行数,进行完全对齐(左右对齐);对于最后一行,以及一行中只有一个单词的行数,进行左对齐
那么其实问题很简单,可以分两步来进行
- 判断每一行中包含几个单词,这步可以同时进行分组
- 判断这一行是需要完全对齐还是左对齐
那就可以按照这两步来撰写代码了
代码
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
int num = words.length;
int i = 0;
int length = 0;
int curr = 0;
int flag = 0;
List<List<String>> temp = new ArrayList<>();
List<String> add = new ArrayList<>();
for(i = 0; i < num; i++){
length += (flag + words[i].length());
if(length > maxWidth){
length = 0;
flag = 0;
i--;
curr++;
temp.add(new ArrayList<>(add));
add = new ArrayList<>();
continue;
}
add.add(words[i]);
flag = 1;
}
List<String> answer = new ArrayList<>();
temp.add(new ArrayList<>(add));
for(i = 0; i < temp.size() - 1; i++){
int len = 0;
for(int j = 0; j < temp.get(i).size(); j++){
len += temp.get(i).get(j).length();
}
int space_num = maxWidth - len;
String a = "";
a = a + temp.get(i).get(0);
if(temp.get(i).size() == 1){
while(a.length() < maxWidth)
a = a + " ";
answer.add(a);
continue;
}
int addition_space = space_num % (temp.get(i).size() - 1);
int every_space = space_num / (temp.get(i).size() - 1);
String ad = "";
for(int j = 0; j < every_space; j++){
ad = ad + " ";
}
for(int j = 1; j < temp.get(i).size(); j++){
if(addition_space != 0){
a = a + " ";
addition_space --;
}
a = a + ad;
a = a + temp.get(i).get(j);
}
answer.add(a);
}
String a = temp.get(i).get(0);
for(int j = 1; j < temp.get(i).size(); j++){
a = a + " ";
a = a + temp.get(i).get(j);
}
while(a.length() < maxWidth)
a = a + " ";
answer.add(a);
return answer;
}
}
代码其实有很大的改进空间,但太晚了就没看别人的算法orz
就这个思想的这份代码而言,可以对左对齐和完全对齐做一个函数封装,代码可读性会强一点。