Skip to content

Commit 8e07bc3

Browse files
committed
feat: add solutions to lc problem: No.0068
No.0068.Text Justification
1 parent 9021c0a commit 8e07bc3

File tree

5 files changed

+442
-30
lines changed

5 files changed

+442
-30
lines changed

solution/0000-0099/0068.Text Justification/README.md

+153
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,113 @@ maxWidth = 20
7979
<!-- 这里可写当前语言的特殊实现逻辑 -->
8080

8181
```python
82+
class Solution:
83+
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
84+
def partition(n, cnt):
85+
res = []
86+
base, mod = divmod(n, cnt)
87+
i = j = 0
88+
while i < cnt:
89+
t = [' ' * base]
90+
if j < mod:
91+
t.append(' ')
92+
res.append(''.join(t))
93+
i, j = i + 1, j + 1
94+
return res
8295

96+
ans = []
97+
i, n = 0, len(words)
98+
while i < n:
99+
t = []
100+
cnt = len(words[i])
101+
t.append(words[i])
102+
i += 1
103+
while i < n and cnt + 1 + len(words[i]) <= maxWidth:
104+
cnt += 1 + len(words[i])
105+
t.append(words[i])
106+
i += 1
107+
if i == n or len(t) == 1:
108+
# this is the last line or only one word in a line
109+
left = ' '.join(t)
110+
right = ' ' * (maxWidth - len(left))
111+
ans.append(left + right)
112+
if i == n:
113+
break
114+
continue
115+
words_width = cnt - len(t) + 1
116+
space_width = maxWidth - words_width
117+
spaces = partition(space_width, len(t) - 1)
118+
sb = [t[0]]
119+
for j in range(len(t) - 1):
120+
sb.append(spaces[j])
121+
sb.append(t[j + 1])
122+
ans.append(''.join(sb))
123+
return ans
83124
```
84125

85126
### **Java**
86127

87128
<!-- 这里可写当前语言的特殊实现逻辑 -->
88129

89130
```java
131+
class Solution {
132+
public List<String> fullJustify(String[] words, int maxWidth) {
133+
List<String> ans = new ArrayList<>();
134+
int n = words.length;
135+
for (int i = 0; i < n; ) {
136+
List<String> t = new ArrayList<>();
137+
int cnt = words[i].length();
138+
t.add(words[i++]);
139+
while (i < n && cnt + 1 + words[i].length() <= maxWidth) {
140+
cnt += 1 + words[i].length();
141+
t.add(words[i++]);
142+
}
143+
if (i == n || t.size() == 1) {
144+
// this is the last line or only one word in a line
145+
String left = String.join(" ", t);
146+
String right = blank(maxWidth - left.length());
147+
ans.add(left + right);
148+
if (i == n) {
149+
break;
150+
}
151+
continue;
152+
}
153+
154+
int wordsWidth = cnt - t.size() + 1;
155+
int spaceWidth = maxWidth - wordsWidth;
156+
List<String> spaces = partition(spaceWidth, t.size() - 1);
157+
StringBuilder sb = new StringBuilder(t.get(0));
158+
for (int j = 0; j < t.size() - 1; ++j) {
159+
sb.append(spaces.get(j));
160+
sb.append(t.get(j + 1));
161+
}
162+
ans.add(sb.toString());
163+
}
164+
return ans;
165+
}
166+
167+
private List<String> partition(int n, int cnt) {
168+
List<String> ans = new ArrayList<>();
169+
int base = n / cnt;
170+
int mod = n % cnt;
171+
for (int i = 0, j = 0; i < cnt; ++i, ++j) {
172+
StringBuilder sb = new StringBuilder(blank(base));
173+
if (j < mod) {
174+
sb.append(' ');
175+
}
176+
ans.add(sb.toString());
177+
}
178+
return ans;
179+
}
90180

181+
private String blank(int n) {
182+
StringBuilder sb = new StringBuilder();
183+
while (n-- > 0) {
184+
sb.append(' ');
185+
}
186+
return sb.toString();
187+
}
188+
}
91189
```
92190

93191
### **C++**
@@ -136,4 +234,59 @@ public:
136234
};
137235
```
138236
237+
### **Go**
238+
239+
```go
240+
func fullJustify(words []string, maxWidth int) []string {
241+
partition := func(n, cnt int) []string {
242+
var res []string
243+
base, mod := n/cnt, n%cnt
244+
for i, j := 0, 0; i < cnt; i, j = i+1, j+1 {
245+
t := strings.Repeat(" ", base)
246+
if j < mod {
247+
t += " "
248+
}
249+
res = append(res, t)
250+
}
251+
return res
252+
}
253+
254+
var ans []string
255+
for i, n := 0, len(words); i < n; {
256+
t := []string{words[i]}
257+
cnt := len(words[i])
258+
i++
259+
for i < n && cnt+1+len(words[i]) <= maxWidth {
260+
cnt += 1 + len(words[i])
261+
t = append(t, words[i])
262+
i++
263+
}
264+
if i == n || len(t) == 1 {
265+
left := strings.Join(t, " ")
266+
right := strings.Repeat(" ", maxWidth-len(left))
267+
ans = append(ans, left+right)
268+
if i == n {
269+
break
270+
}
271+
continue
272+
}
273+
wordsWidth := cnt - len(t) + 1
274+
spaceWidth := maxWidth - wordsWidth
275+
spaces := partition(spaceWidth, len(t)-1)
276+
sb := t[0]
277+
for j := 0; j < len(t)-1; j++ {
278+
sb += spaces[j] + t[j+1]
279+
}
280+
ans = append(ans, sb)
281+
}
282+
return ans
283+
}
284+
```
285+
286+
### **...**
287+
288+
```
289+
290+
```
291+
139292
<!-- tabs:end -->

solution/0000-0099/0068.Text Justification/README_EN.md

+153
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,111 @@ Note that the second line is also left-justified becase it contains only one wor
7777
### **Python3**
7878

7979
```python
80+
class Solution:
81+
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
82+
def partition(n, cnt):
83+
res = []
84+
base, mod = divmod(n, cnt)
85+
i = j = 0
86+
while i < cnt:
87+
t = [' ' * base]
88+
if j < mod:
89+
t.append(' ')
90+
res.append(''.join(t))
91+
i, j = i + 1, j + 1
92+
return res
8093

94+
ans = []
95+
i, n = 0, len(words)
96+
while i < n:
97+
t = []
98+
cnt = len(words[i])
99+
t.append(words[i])
100+
i += 1
101+
while i < n and cnt + 1 + len(words[i]) <= maxWidth:
102+
cnt += 1 + len(words[i])
103+
t.append(words[i])
104+
i += 1
105+
if i == n or len(t) == 1:
106+
# this is the last line or only one word in a line
107+
left = ' '.join(t)
108+
right = ' ' * (maxWidth - len(left))
109+
ans.append(left + right)
110+
if i == n:
111+
break
112+
continue
113+
words_width = cnt - len(t) + 1
114+
space_width = maxWidth - words_width
115+
spaces = partition(space_width, len(t) - 1)
116+
sb = [t[0]]
117+
for j in range(len(t) - 1):
118+
sb.append(spaces[j])
119+
sb.append(t[j + 1])
120+
ans.append(''.join(sb))
121+
return ans
81122
```
82123

83124
### **Java**
84125

85126
```java
127+
class Solution {
128+
public List<String> fullJustify(String[] words, int maxWidth) {
129+
List<String> ans = new ArrayList<>();
130+
int n = words.length;
131+
for (int i = 0; i < n; ) {
132+
List<String> t = new ArrayList<>();
133+
int cnt = words[i].length();
134+
t.add(words[i++]);
135+
while (i < n && cnt + 1 + words[i].length() <= maxWidth) {
136+
cnt += 1 + words[i].length();
137+
t.add(words[i++]);
138+
}
139+
if (i == n || t.size() == 1) {
140+
// this is the last line or only one word in a line
141+
String left = String.join(" ", t);
142+
String right = blank(maxWidth - left.length());
143+
ans.add(left + right);
144+
if (i == n) {
145+
break;
146+
}
147+
continue;
148+
}
149+
150+
int wordsWidth = cnt - t.size() + 1;
151+
int spaceWidth = maxWidth - wordsWidth;
152+
List<String> spaces = partition(spaceWidth, t.size() - 1);
153+
StringBuilder sb = new StringBuilder(t.get(0));
154+
for (int j = 0; j < t.size() - 1; ++j) {
155+
sb.append(spaces.get(j));
156+
sb.append(t.get(j + 1));
157+
}
158+
ans.add(sb.toString());
159+
}
160+
return ans;
161+
}
162+
163+
private List<String> partition(int n, int cnt) {
164+
List<String> ans = new ArrayList<>();
165+
int base = n / cnt;
166+
int mod = n % cnt;
167+
for (int i = 0, j = 0; i < cnt; ++i, ++j) {
168+
StringBuilder sb = new StringBuilder(blank(base));
169+
if (j < mod) {
170+
sb.append(' ');
171+
}
172+
ans.add(sb.toString());
173+
}
174+
return ans;
175+
}
86176

177+
private String blank(int n) {
178+
StringBuilder sb = new StringBuilder();
179+
while (n-- > 0) {
180+
sb.append(' ');
181+
}
182+
return sb.toString();
183+
}
184+
}
87185
```
88186

89187
### **C++**
@@ -132,4 +230,59 @@ public:
132230
};
133231
```
134232
233+
### **Go**
234+
235+
```go
236+
func fullJustify(words []string, maxWidth int) []string {
237+
partition := func(n, cnt int) []string {
238+
var res []string
239+
base, mod := n/cnt, n%cnt
240+
for i, j := 0, 0; i < cnt; i, j = i+1, j+1 {
241+
t := strings.Repeat(" ", base)
242+
if j < mod {
243+
t += " "
244+
}
245+
res = append(res, t)
246+
}
247+
return res
248+
}
249+
250+
var ans []string
251+
for i, n := 0, len(words); i < n; {
252+
t := []string{words[i]}
253+
cnt := len(words[i])
254+
i++
255+
for i < n && cnt+1+len(words[i]) <= maxWidth {
256+
cnt += 1 + len(words[i])
257+
t = append(t, words[i])
258+
i++
259+
}
260+
if i == n || len(t) == 1 {
261+
left := strings.Join(t, " ")
262+
right := strings.Repeat(" ", maxWidth-len(left))
263+
ans = append(ans, left+right)
264+
if i == n {
265+
break
266+
}
267+
continue
268+
}
269+
wordsWidth := cnt - len(t) + 1
270+
spaceWidth := maxWidth - wordsWidth
271+
spaces := partition(spaceWidth, len(t)-1)
272+
sb := t[0]
273+
for j := 0; j < len(t)-1; j++ {
274+
sb += spaces[j] + t[j+1]
275+
}
276+
ans = append(ans, sb)
277+
}
278+
return ans
279+
}
280+
```
281+
282+
### **...**
283+
284+
```
285+
286+
```
287+
135288
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func fullJustify(words []string, maxWidth int) []string {
2+
partition := func(n, cnt int) []string {
3+
var res []string
4+
base, mod := n/cnt, n%cnt
5+
for i, j := 0, 0; i < cnt; i, j = i+1, j+1 {
6+
t := strings.Repeat(" ", base)
7+
if j < mod {
8+
t += " "
9+
}
10+
res = append(res, t)
11+
}
12+
return res
13+
}
14+
15+
var ans []string
16+
for i, n := 0, len(words); i < n; {
17+
t := []string{words[i]}
18+
cnt := len(words[i])
19+
i++
20+
for i < n && cnt+1+len(words[i]) <= maxWidth {
21+
cnt += 1 + len(words[i])
22+
t = append(t, words[i])
23+
i++
24+
}
25+
if i == n || len(t) == 1 {
26+
left := strings.Join(t, " ")
27+
right := strings.Repeat(" ", maxWidth-len(left))
28+
ans = append(ans, left+right)
29+
if i == n {
30+
break
31+
}
32+
continue
33+
}
34+
wordsWidth := cnt - len(t) + 1
35+
spaceWidth := maxWidth - wordsWidth
36+
spaces := partition(spaceWidth, len(t)-1)
37+
sb := t[0]
38+
for j := 0; j < len(t)-1; j++ {
39+
sb += spaces[j] + t[j+1]
40+
}
41+
ans = append(ans, sb)
42+
}
43+
return ans
44+
}

0 commit comments

Comments
 (0)