Skip to content

Commit 3c8f2e2

Browse files
refactor 68
1 parent 9315c1f commit 3c8f2e2

File tree

1 file changed

+42
-70
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-70
lines changed

src/main/java/com/fishercoder/solutions/_68.java

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,59 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
/**
7-
* 68. Text Justification
8-
9-
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.
10-
You should pack your words in a greedy approach; that is, pack as many words as you can in each line.
11-
Pad extra spaces ' ' when necessary so that each line has exactly L characters.
12-
Extra spaces between words should be distributed as evenly as possible.
13-
If the number of spaces on a line do not divide evenly between words,
14-
the empty slots on the left will be assigned more spaces than the slots on the right.
15-
For the last line of text, it should be left justified and no extra space is inserted between words.
16-
17-
For example,
18-
words: ["This", "is", "an", "example", "of", "text", "justification."]
19-
L: 16.
20-
21-
Return the formatted lines as:
22-
[
23-
"This is an",
24-
"example of text",
25-
"justification. "
26-
]
27-
28-
Note: Each word is guaranteed not to exceed L in length.
29-
30-
Corner Cases:
31-
A line other than the last line might contain only one word. What should you do in this case?
32-
In this case, that line should be left-justified.
33-
*/
346
public class _68 {
357

36-
public static class Solution1 {
37-
public List<String> fullJustify(String[] words, int L) {
38-
ArrayList<String> result = new ArrayList();
39-
if (words == null || words.length == 0) {
40-
return result;
41-
}
42-
int count = 0;
43-
int last = 0;
44-
for (int i = 0; i < words.length; i++) {
45-
if (count + words[i].length() + (i - last) > L) {
46-
int spaceNum = 0;
47-
int extraNum = 0;
48-
if (i - last - 1 > 0) {
49-
spaceNum = (L - count) / (i - last - 1);
50-
extraNum = (L - count) % (i - last - 1);
51-
}
52-
StringBuilder sb = new StringBuilder();
53-
for (int j = last; j < i; j++) {
54-
sb.append(words[j]);
55-
if (j < i - 1) {
56-
for (int k = 0; k < spaceNum; k++) {
57-
sb.append(" ");
58-
}
59-
if (extraNum > 0) {
60-
sb.append(" ");
8+
public static class Solution1 {
9+
public List<String> fullJustify(String[] words, int L) {
10+
ArrayList<String> result = new ArrayList();
11+
if (words == null || words.length == 0) {
12+
return result;
13+
}
14+
int count = 0;
15+
int last = 0;
16+
for (int i = 0; i < words.length; i++) {
17+
if (count + words[i].length() + (i - last) > L) {
18+
int spaceNum = 0;
19+
int extraNum = 0;
20+
if (i - last - 1 > 0) {
21+
spaceNum = (L - count) / (i - last - 1);
22+
extraNum = (L - count) % (i - last - 1);
23+
}
24+
StringBuilder sb = new StringBuilder();
25+
for (int j = last; j < i; j++) {
26+
sb.append(words[j]);
27+
if (j < i - 1) {
28+
for (int k = 0; k < spaceNum; k++) {
29+
sb.append(" ");
30+
}
31+
if (extraNum > 0) {
32+
sb.append(" ");
33+
}
34+
extraNum--;
6135
}
62-
extraNum--;
6336
}
37+
for (int j = sb.length(); j < L; j++) {
38+
sb.append(" ");
39+
}
40+
result.add(sb.toString());
41+
count = 0;
42+
last = i;
6443
}
65-
for (int j = sb.length(); j < L; j++) {
44+
count += words[i].length();
45+
}
46+
StringBuilder sb = new StringBuilder();
47+
for (int i = last; i < words.length; i++) {
48+
sb.append(words[i]);
49+
if (sb.length() < L) {
6650
sb.append(" ");
6751
}
68-
result.add(sb.toString());
69-
count = 0;
70-
last = i;
7152
}
72-
count += words[i].length();
73-
}
74-
StringBuilder sb = new StringBuilder();
75-
for (int i = last; i < words.length; i++) {
76-
sb.append(words[i]);
77-
if (sb.length() < L) {
53+
for (int i = sb.length(); i < L; i++) {
7854
sb.append(" ");
7955
}
56+
result.add(sb.toString());
57+
return result;
8058
}
81-
for (int i = sb.length(); i < L; i++) {
82-
sb.append(" ");
83-
}
84-
result.add(sb.toString());
85-
return result;
8659
}
87-
}
8860

8961
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy