题目来源:https://leetcode.com/problems/text-justification/
问题描述
68. Text Justification
Hard
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.
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 "
]
------------------------------------------------------------
题意
把word集合排版到固定长度的行。规则如下:
(1)最后一行左对齐
(2)如果一行只有一个word,则左对齐
(3)如果不是上述两种情况,则居中。具体来说,空格要均匀分布在各个word的间隔,如果不能均匀分布,则靠左的间隔的空格可以多一个。
------------------------------------------------------------
思路
模拟题
------------------------------------------------------------
代码
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> ret = new LinkedList<String>();
int n = words.length, i = 0, wordcnt = 0, charcnt = 0, spaces = 1, extraSpaces = 0;
if (n == 0)
{
char[] chs = new char[maxWidth];
Arrays.fill(chs, ' ');
ret.add(new String(chs));
return ret;
}
while (i < n)
{
wordcnt = 0;
charcnt = 0;
while (i < n && charcnt + wordcnt - 1 <= maxWidth)
{
charcnt += words[i].length();
wordcnt++;
i++;
}
if (charcnt + wordcnt - 1 > maxWidth)
{
charcnt -= words[i-1].length();
wordcnt--;
i--;
}
if (wordcnt > 1 && i < n)
{
spaces = (maxWidth-charcnt)/(wordcnt-1);
char[] chs = new char[spaces];
Arrays.fill(chs, ' ');
extraSpaces = (maxWidth-charcnt)%(wordcnt-1);
StringBuilder sb = new StringBuilder();
for (int j = i - wordcnt; j < i; j++)
{
sb.append(words[j]);
if (j-i+wordcnt<extraSpaces)
{
sb.append(new String(chs));
sb.append(" ");
}
else if (j < i-1)
{
sb.append(new String(chs));
}
}
ret.add(sb.toString());
}
else if (i == n)
{
StringBuilder sb = new StringBuilder();
spaces = maxWidth;
for (int j = i - wordcnt; j < i; j++)
{
sb.append(words[j]);
spaces -= words[j].length();
if (j < i - 1)
{
sb.append(" ");
spaces--;
}
else
{
char[] chs = new char[spaces];
Arrays.fill(chs, ' ');
sb.append(chs);
}
}
ret.add(sb.toString());
}
else
{
char[] chs = new char[maxWidth-charcnt];
Arrays.fill(chs, ' ');
ret.add(words[i-1]+new String(chs));
}
}
return ret;
}
}