Given an array of words and a length L, format the text such that each line has exactly L 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 exactlyL 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.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. " ]
Note: Each word is guaranteed not to exceed L in length.
- A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
Code:
/**
*
*/
package From61;
import java.util.ArrayList;
import java.util.List;
/**
* @author MohnSnow
* @time 2015年6月29日 下午5:38:33
*
*/
public class LeetCode68 {
/**
* @param argsmengdx
* -fnst
*/
public static List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
int count = 0;
StringBuffer temp = new StringBuffer();
for (int i = 0; i < words.length; i++) {
if (words[i].length() < maxWidth - count) {
temp.append(words[i]);
temp.append(" ");
count = count + words[i].length() + 1;
} else {
res.add(temp.toString());
temp.delete(0, temp.capacity());
temp.append(words[i]);
temp.append(" ");
count = 0;
count = words[i].length() + 1;
}
}
res.add(temp.toString());
return res;
}
//https://leetcode.com/discuss/25949/simple-java-solution
public static List<String> fullJustify1(String[] words, int L)
{
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < L; ++i)
{
sb.append(" ");
}
System.out.println("sb: " + sb.toString() + "sb.length():" + sb.length());//为初始字符串加上空格
final String pads = sb.toString();
final List<String> strs = new ArrayList<>();
for (int i = 0, sum = 0, j = 0; i < words.length; i = j)
{
for (j = i + 1, sum = words[i].length(); j < words.length && sum + j - i + words[j].length() <= L; ++j)
{
sum += words[j].length();
}
final StringBuilder l = new StringBuilder();
final int n = j - 1 - i;
final int m = (j == words.length || 0 == n) ? 1 : ((L - sum) / n);
final int b = (j == words.length) ? 0 : (L - sum - m * n);
for (int k = i; k < j - 1; ++k)
{
l.append(words[k]);
l.append(pads.substring(0, (k - i < b) ? (m + 1) : m));
}
l.append(words[j - 1]);
if (j == words.length || 0 == n)
{
l.append(pads.substring(0, L - sum - n));
}
strs.add(l.toString());
}
return strs;
}
public static void main(String[] args) {
String[] words = { "This", "is", "an", "example", "of", "text", "justification." };
int maxWidth = 16;
System.out.println("addBinary: " + fullJustify(words, maxWidth));
System.out.println("addBinary: " + fullJustify1(words, maxWidth));
}
}