|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
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 |
| - */ |
34 | 6 | public class _68 {
|
35 | 7 |
|
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--; |
61 | 35 | }
|
62 |
| - extraNum--; |
63 | 36 | }
|
| 37 | + for (int j = sb.length(); j < L; j++) { |
| 38 | + sb.append(" "); |
| 39 | + } |
| 40 | + result.add(sb.toString()); |
| 41 | + count = 0; |
| 42 | + last = i; |
64 | 43 | }
|
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) { |
66 | 50 | sb.append(" ");
|
67 | 51 | }
|
68 |
| - result.add(sb.toString()); |
69 |
| - count = 0; |
70 |
| - last = i; |
71 | 52 | }
|
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++) { |
78 | 54 | sb.append(" ");
|
79 | 55 | }
|
| 56 | + result.add(sb.toString()); |
| 57 | + return result; |
80 | 58 | }
|
81 |
| - for (int i = sb.length(); i < L; i++) { |
82 |
| - sb.append(" "); |
83 |
| - } |
84 |
| - result.add(sb.toString()); |
85 |
| - return result; |
86 | 59 | }
|
87 |
| - } |
88 | 60 |
|
89 | 61 | }
|
0 commit comments