Skip to content

Commit 12d9074

Browse files
committed
feat: add 068
1 parent f2fa69f commit 12d9074

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
|25|[Reverse Nodes in k-Group][025]|Linked List|
9292
|44|[Reverse Nodes in k-Group][044]|String, Dynamic Programming, Backtracking, Greedy|
9393
|57|[Insert Interval][057]|Array, Sort|
94+
|68|[Text Justification][068]|String|
9495

9596

9697

@@ -152,3 +153,4 @@
152153
[025]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/025/README.md
153154
[044]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/044/README.md
154155
[057]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/057/README.md
156+
[068]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/068/README.md

note/068/README.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [Text Justification][title]
2+
3+
## Description
4+
5+
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.
6+
7+
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.
8+
9+
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.
10+
11+
For the last line of text, it should be left justified and no extra space is inserted between words.
12+
13+
For example,
14+
**words**: `["This", "is", "an", "example", "of", "text", "justification."]`
15+
**L**: `16`.
16+
17+
Return the formatted lines as:
18+
19+
```
20+
[
21+
"This is an",
22+
"example of text",
23+
"justification. "
24+
]
25+
26+
```
27+
28+
**Note:** Each word is guaranteed not to exceed *L* in length.
29+
30+
**Corner Cases:**
31+
32+
- A line other than the last line might contain only one word. What should you do in this case?
33+
In this case, that line should be left-justified.
34+
35+
**Tags:** String
36+
37+
38+
## 思路
39+
40+
题意是给你一组单词和最大行宽,让你对齐他们,对齐的规则就是尽可能一行可以放下足够多的单词,如果最后有多余的空格,那就把空格均匀地插入到单词之间,如果不能平分的话,那就从左开始依次多插一个空格,最后一行单词之间就正常地一个空格即可,如果凑不到最大行宽,那就在末尾补充空格即可,描述地比较差,不懂的话其实看看demo也就懂了哈。题还是比较坑的,毕竟踩的比赞的人多,我也是靠模拟老老实实做出来的,求出可以最多插入空格数,然后用它除以可以插入的槽数获取每个单词之间的空格,它两取余的话就是最面需要多插入一个空格的个数,最后一行的话就单独处理即可。
41+
42+
```java
43+
class Solution {
44+
public List<String> fullJustify(String[] words, int maxWidth) {
45+
int len = words.length;
46+
List<String> ans = new ArrayList<>();
47+
StringBuilder spaces = new StringBuilder();
48+
for (int i = 0; i < maxWidth; ++i) {
49+
spaces.append(" ");
50+
}
51+
int curLen = -1, start = 0;
52+
for (int i = 0; i < len; ++i) {
53+
if (curLen + words[i].length() + 1 <= maxWidth) {
54+
curLen += words[i].length() + 1;
55+
} else {
56+
StringBuilder sub = new StringBuilder(words[start]);
57+
int rest = maxWidth - curLen;
58+
int l = i - start - 1;
59+
if (l <= 0) {
60+
sub.append(spaces.substring(0, rest));
61+
} else {
62+
int m = rest / l + 1;
63+
int mod = rest % l;
64+
for (int j = start + 1; j < i; ++j) {
65+
if (mod-- > 0) {
66+
sub.append(spaces.substring(0, m + 1)).append(words[j]);
67+
} else {
68+
sub.append(spaces.substring(0, m)).append(words[j]);
69+
}
70+
}
71+
}
72+
ans.add(sub.toString());
73+
start = i;
74+
curLen = words[i].length();
75+
}
76+
}
77+
StringBuilder sub = new StringBuilder(words[start]);
78+
for (int i = start + 1; i < len; ++i) {
79+
sub.append(" ").append(words[i]);
80+
}
81+
ans.add(sub + spaces.substring(0, maxWidth - sub.length()));
82+
return ans;
83+
}
84+
}
85+
```
86+
87+
88+
## 结语
89+
90+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
91+
92+
93+
94+
[title]: https://leetcode.com/problems/text-justification
95+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.blankj.hard._068;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/11/01
11+
* desc :
12+
* </pre>
13+
*/
14+
public class Solution {
15+
16+
public List<String> fullJustify(String[] words, int maxWidth) {
17+
int len = words.length;
18+
List<String> ans = new ArrayList<>();
19+
StringBuilder spaces = new StringBuilder();
20+
for (int i = 0; i < maxWidth; ++i) {
21+
spaces.append(" ");
22+
}
23+
int curLen = -1, start = 0;
24+
for (int i = 0; i < len; ++i) {
25+
if (curLen + words[i].length() + 1 <= maxWidth) {
26+
curLen += words[i].length() + 1;
27+
} else {
28+
StringBuilder sub = new StringBuilder(words[start]);
29+
int rest = maxWidth - curLen;
30+
int l = i - start - 1;
31+
if (l <= 0) {
32+
sub.append(spaces.substring(0, rest));
33+
} else {
34+
int m = rest / l + 1;
35+
int mod = rest % l;
36+
for (int j = start + 1; j < i; ++j) {
37+
if (mod-- > 0) {
38+
sub.append(spaces.substring(0, m + 1)).append(words[j]);
39+
} else {
40+
sub.append(spaces.substring(0, m)).append(words[j]);
41+
}
42+
}
43+
}
44+
ans.add(sub.toString());
45+
start = i;
46+
curLen = words[i].length();
47+
}
48+
}
49+
StringBuilder sub = new StringBuilder(words[start]);
50+
for (int i = start + 1; i < len; ++i) {
51+
sub.append(" ").append(words[i]);
52+
}
53+
ans.add(sub + spaces.substring(0, maxWidth - sub.length()));
54+
return ans;
55+
}
56+
57+
public static void main(String[] args) {
58+
Solution solution = new Solution();
59+
System.out.println(solution.fullJustify(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16));
60+
}
61+
}

0 commit comments

Comments
 (0)