|
7 | 7 |
|
8 | 8 | class TextJustification { |
9 | 9 | func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] { |
10 | | - var res = [String]() |
11 | | - var last = 0, currentLineLength = 0 |
12 | | - |
| 10 | + var res = [String](), rowStart = 0, currentLen = 0 |
| 11 | + |
13 | 12 | for (i, word) in words.enumerated() { |
14 | | - if currentLineLength + word.count + (i - last) > maxWidth { |
15 | | - |
16 | | - res.append(buildLine(words, last, i - 1, maxWidth, currentLineLength)) |
17 | | - |
18 | | - last = i |
19 | | - currentLineLength = 0 |
| 13 | + |
| 14 | + if currentLen + word.count + (i - rowStart) > maxWidth { |
| 15 | + res.append(buildRow(rowStart, i - 1, words, maxWidth, currentLen)) |
| 16 | + |
| 17 | + rowStart = i |
| 18 | + currentLen = 0 |
20 | 19 | } |
21 | | - |
22 | | - currentLineLength += word.count |
| 20 | + |
| 21 | + currentLen += word.count |
23 | 22 | } |
24 | | - |
25 | | - res.append(buildLastLine(words, last, words.count - 1, maxWidth)) |
26 | | - |
| 23 | + |
| 24 | + res.append(buildLastRow(rowStart, words, maxWidth)) |
| 25 | + |
27 | 26 | return res |
28 | 27 | } |
29 | | - |
30 | | - fileprivate func buildLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int, _ currentLineLength: Int) -> String { |
31 | | - var line = "" |
32 | | - var extraSpaceNum = 0, spaceNum = 0 |
33 | | - |
| 28 | + |
| 29 | + private func buildRow(_ start: Int, _ end: Int, _ words: [String], _ maxWidth: Int, _ currentLen: Int) -> String { |
| 30 | + var res = "", spaceNum = 0, extraSpaceNum = 0 |
| 31 | + |
34 | 32 | if end > start { |
35 | | - extraSpaceNum = (maxWidth - currentLineLength) % (end - start) |
36 | | - spaceNum = (maxWidth - currentLineLength) / (end - start) |
| 33 | + spaceNum = (maxWidth - currentLen) / (end - start) |
| 34 | + extraSpaceNum = (maxWidth - currentLen) % (end - start) |
37 | 35 | } else { |
38 | | - spaceNum = maxWidth - currentLineLength |
| 36 | + spaceNum = maxWidth - currentLen |
39 | 37 | } |
40 | | - |
| 38 | + |
41 | 39 | for i in start...end { |
42 | | - line.append(words[i]) |
43 | | - |
| 40 | + res += words[i] |
| 41 | + |
44 | 42 | if start != end && i == end { |
45 | 43 | break |
46 | | - } |
47 | | - |
48 | | - for _ in 0..<spaceNum { |
49 | | - line.append(" ") |
50 | 44 | } |
51 | | - |
| 45 | + |
| 46 | + res.append(String(repeating: " ", count: spaceNum)) |
52 | 47 | if extraSpaceNum > 0 { |
53 | | - line.append(" ") |
| 48 | + res.append(" ") |
54 | 49 | extraSpaceNum -= 1 |
55 | 50 | } |
56 | 51 | } |
57 | | - |
58 | | - return line |
| 52 | + |
| 53 | + return res |
59 | 54 | } |
60 | | - |
61 | | - fileprivate func buildLastLine(_ words: [String], _ start: Int, _ end: Int, _ maxWidth: Int) -> String { |
62 | | - var line = "" |
63 | | - |
64 | | - for i in start...end { |
65 | | - line.append(words[i]) |
66 | | - |
67 | | - if i < end { |
68 | | - line.append(" ") |
| 55 | + |
| 56 | + private func buildLastRow(_ start: Int, _ words: [String], _ maxWidth: Int) -> String { |
| 57 | + var res = "" |
| 58 | + |
| 59 | + for i in start..<words.count { |
| 60 | + res += words[i] |
| 61 | + |
| 62 | + if i + 1 < words.count { |
| 63 | + res.append(" ") |
69 | 64 | } |
70 | 65 | } |
71 | | - |
72 | | - while line.count < maxWidth { |
73 | | - line.append(" ") |
| 66 | + |
| 67 | + if res.count < maxWidth { |
| 68 | + res.append(String(repeating: " ", count: maxWidth - res.count)) |
74 | 69 | } |
75 | | - |
76 | | - return line |
| 70 | + |
| 71 | + return res |
77 | 72 | } |
78 | 73 | } |
0 commit comments