Skip to content

Commit fe62411

Browse files
refactor 68
1 parent e9f7a4b commit fe62411

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import java.util.List;
55

66
/**
7-
* 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.
8-
9-
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.
10-
11-
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.
7+
* 68. Text Justification
128
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.
1315
For the last line of text, it should be left justified and no extra space is inserted between words.
1416
1517
For example,
@@ -22,17 +24,17 @@
2224
"example of text",
2325
"justification. "
2426
]
25-
Note: Each word is guaranteed not to exceed L in length.
2627
27-
click to show corner cases.
28+
Note: Each word is guaranteed not to exceed L in length.
2829
2930
Corner Cases:
3031
A line other than the last line might contain only one word. What should you do in this case?
3132
In this case, that line should be left-justified.
3233
*/
3334
public class _68 {
3435

35-
public static List<String> fullJustify(String[] words, int L) {
36+
public static class Solution1 {
37+
public List<String> fullJustify(String[] words, int L) {
3638
ArrayList<String> result = new ArrayList();
3739
if (words == null || words.length == 0) {
3840
return result;
@@ -82,14 +84,6 @@ public static List<String> fullJustify(String[] words, int L) {
8284
result.add(sb.toString());
8385
return result;
8486
}
87+
}
8588

86-
public static void main(String... args) {
87-
// String[] words = new String[]{"This", "is", "an", "example", "of", "text", "justification."};
88-
String[] words = new String[]{"This", "is", "a", "good", "test!", "\n", "What", "do", "you", "\n", "think?", "\n", "I", "think", "so", "too!"};
89-
int L = 16;
90-
List<String> result = fullJustify(words, L);
91-
for (String str : result) {
92-
System.out.println(str);
93-
}
94-
}
9589
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._68;
4+
import java.util.Arrays;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _68Test {
11+
private static _68.Solution1 solution1;
12+
private static String[] words;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _68.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
words =
22+
new String[] {"This", "is", "a", "good", "test!", "\n", "What", "do", "you", "\n", "think?",
23+
"\n", "I", "think", "so", "too!"};
24+
assertEquals(Arrays.asList(
25+
"This is a good",
26+
"test! \n What do",
27+
"you \n think? \n I",
28+
"think so too! "), solution1.fullJustify(words, 16));
29+
}
30+
31+
@Test
32+
public void test2() {
33+
words = new String[] {"This", "is", "an", "example", "of", "text", "justification."};
34+
assertEquals(Arrays.asList(
35+
"This is an",
36+
"example of text",
37+
"justification. "), solution1.fullJustify(words, 16));
38+
}
39+
}

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