[leetcode]68. Text Justification@Java解题报告
2017-07-24 12:38
417 查看
https://leetcode.com/problems/text-justification/#/description
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
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:
L:
Return the formatted lines as:
Note: Each word is guaranteed not to exceed L in length.
package go.jacob.day724;
import java.util.ArrayList;
import java.util.List;
/**
* 68. Text Justification
*
* @author Jacob
*
* 理解题意:首先要做的就是确定每一行能放下的单词数,这个不难, 就是比较n个单词的长度和加上n -1个空格的长度跟给定的长度L来比较即可,
* 找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数, 是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,
* 就要在每个单词后面插入这些空格,这里有两种情况, 比如某一行有两个单词"to"和
* "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to a", 如果是最后一行,则应该输出 "to a",
* 所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。
* 最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,
* 那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,
* 能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里
*
*/
public class Demo1 {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<String>();
int n = words.length;
int i = 0;
while (i < n) {
StringBuilder sb = new StringBuilder();
int last = i + 1;
int len = words[i].length();
while (last < n && len + 1 + words[last].length() <= maxWidth) {
len += 1 + words[last].length();
last++;
}
// 最后一行
if (last == n) {
for (int j = i; j < n; j++) {
sb.append(words[j] + " ");
}
sb.deleteCharAt(sb.length() - 1);
for (int j = sb.length(); j < maxWidth; j++) {
sb.append(" ");
}
} else {
// 只有一个word
if (last - i == 1) {
sb.append(words[i]);
for (int j = words[i].length(); j < maxWidth; j++)
sb.append(" ");
} else {// 有多个单词
int wordNum = last - i;
int wordTotal = 0;
for (int j = i; j < last; j++) {
wordTotal += words[j].length();
}
// eachSpace为每个单词间的空格数;r是余数,表示前r个空格数为eachSpace+1
int eachSpace = (maxWidth - wordTotal) / (wordNum - 1);
int r = (maxWidth - wordTotal) % (wordNum - 1);
for (int j = i; j < last; j++) {
sb.append(words[j]);
if (j < last - 1) {
for (int k = 0; k < eachSpace + ((j - i) < r ? 1 : 0); k++) {
sb.append(" ");
}
}
}
}
}
res.add(sb.toString());
i = last;
}
return res;
}
}
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 exactly L 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.
package go.jacob.day724;
import java.util.ArrayList;
import java.util.List;
/**
* 68. Text Justification
*
* @author Jacob
*
* 理解题意:首先要做的就是确定每一行能放下的单词数,这个不难, 就是比较n个单词的长度和加上n -1个空格的长度跟给定的长度L来比较即可,
* 找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数, 是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,
* 就要在每个单词后面插入这些空格,这里有两种情况, 比如某一行有两个单词"to"和
* "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to a", 如果是最后一行,则应该输出 "to a",
* 所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。
* 最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,
* 那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,
* 能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里
*
*/
public class Demo1 {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<String>();
int n = words.length;
int i = 0;
while (i < n) {
StringBuilder sb = new StringBuilder();
int last = i + 1;
int len = words[i].length();
while (last < n && len + 1 + words[last].length() <= maxWidth) {
len += 1 + words[last].length();
last++;
}
// 最后一行
if (last == n) {
for (int j = i; j < n; j++) {
sb.append(words[j] + " ");
}
sb.deleteCharAt(sb.length() - 1);
for (int j = sb.length(); j < maxWidth; j++) {
sb.append(" ");
}
} else {
// 只有一个word
if (last - i == 1) {
sb.append(words[i]);
for (int j = words[i].length(); j < maxWidth; j++)
sb.append(" ");
} else {// 有多个单词
int wordNum = last - i;
int wordTotal = 0;
for (int j = i; j < last; j++) {
wordTotal += words[j].length();
}
// eachSpace为每个单词间的空格数;r是余数,表示前r个空格数为eachSpace+1
int eachSpace = (maxWidth - wordTotal) / (wordNum - 1);
int r = (maxWidth - wordTotal) % (wordNum - 1);
for (int j = i; j < last; j++) {
sb.append(words[j]);
if (j < last - 1) {
for (int k = 0; k < eachSpace + ((j - i) < r ? 1 : 0); k++) {
sb.append(" ");
}
}
}
}
}
res.add(sb.toString());
i = last;
}
return res;
}
}
相关文章推荐
- [leetcode]19. Remove Nth Node From End of List@Java解题报告
- [leetcode]103. Binary Tree Zigzag Level Order Traversal@Java解题报告
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- [leetcode]110. Balanced Binary Tree@Java解题报告
- [leetcode]76. Minimum Window Substring@Java解题报告
- [leetcode]136. Single Number@Java解题报告
- [leetcode]98. Validate Binary Search Tree@Java解题报告
- [leetcode]100. Same Tree@Java解题报告
- 【LeetCode】Merge Two Sorted Lists 解题报告(Java & Python)
- 景岁的Leetcode解题报告:147.Insertion Sort List (Java)
- LeetCode Plus One Java版解题报告
- [leetcode]61. Rotate List@Java解题报告
- 【LeetCode】Hamming Distance 解题报告(java & python)
- 【LeetCode】Linked List Cycle 解题报告(Java & Python)
- [leetcode]215. Kth Largest Element in an Array@Java解题报告
- [leetcode]438. Find All Anagrams in a String@Java解题报告
- Maximum Depth of Binary Tree | leetcode 104 【Java解题报告】
- [leetcode]144. Binary Tree Preorder Traversal@Java解题报告
- 【LeetCode】Two Sum 解题报告(java & python)
- Leetcode 349. Intersection of Two Arrays 解题报告 Python Java