|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 | 6 | /**
|
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 |
12 | 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. |
13 | 15 | For the last line of text, it should be left justified and no extra space is inserted between words.
|
14 | 16 |
|
15 | 17 | For example,
|
|
22 | 24 | "example of text",
|
23 | 25 | "justification. "
|
24 | 26 | ]
|
25 |
| - Note: Each word is guaranteed not to exceed L in length. |
26 | 27 |
|
27 |
| - click to show corner cases. |
| 28 | + Note: Each word is guaranteed not to exceed L in length. |
28 | 29 |
|
29 | 30 | Corner Cases:
|
30 | 31 | A line other than the last line might contain only one word. What should you do in this case?
|
31 | 32 | In this case, that line should be left-justified.
|
32 | 33 | */
|
33 | 34 | public class _68 {
|
34 | 35 |
|
35 |
| - public static List<String> fullJustify(String[] words, int L) { |
| 36 | + public static class Solution1 { |
| 37 | + public List<String> fullJustify(String[] words, int L) { |
36 | 38 | ArrayList<String> result = new ArrayList();
|
37 | 39 | if (words == null || words.length == 0) {
|
38 | 40 | return result;
|
@@ -82,14 +84,6 @@ public static List<String> fullJustify(String[] words, int L) {
|
82 | 84 | result.add(sb.toString());
|
83 | 85 | return result;
|
84 | 86 | }
|
| 87 | + } |
85 | 88 |
|
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 |
| - } |
95 | 89 | }
|
0 commit comments