1
1
package com.blankj.hard._068
2
2
3
3
import com.blankj.ext.print
4
+ import java.lang.Appendable
4
5
5
6
class Solution {
6
7
fun fullJustify (words : Array <String >, maxWidth : Int ): List <String > {
@@ -14,7 +15,7 @@ class Solution {
14
15
sumLength + = words[right++ ].length
15
16
}
16
17
if (right == words.size) {
17
- val lastLine = join(words, left, words.size, " " )
18
+ val lastLine = StringBuilder (). join(words, left, words.size, " " )
18
19
lastLine.append(" " .repeat(maxWidth - lastLine.length))
19
20
res.add(lastLine.toString())
20
21
return res
@@ -32,21 +33,21 @@ class Solution {
32
33
val avgSpaces = numSpaces / (numWords - 1 )
33
34
val countWithextraSpaces = numSpaces % (numWords - 1 )
34
35
res.add(buildString {
35
- append( join(words, left, left + countWithextraSpaces + 1 , " " .repeat(avgSpaces + 1 ) ))
36
+ join(words, left, left + countWithextraSpaces + 1 , " " .repeat(avgSpaces + 1 ))
36
37
append(" " .repeat(avgSpaces))
37
- append( join(words, left + countWithextraSpaces + 1 , right, " " .repeat(avgSpaces) ))
38
+ join(words, left + countWithextraSpaces + 1 , right, " " .repeat(avgSpaces))
38
39
})
39
40
}
40
41
}
41
42
42
43
// Join string from left to (right - 1)
43
- private fun join (words : Array <String >, left : Int , right : Int , separator : String ): StringBuilder {
44
- val builder = StringBuilder (words[left])
44
+ private fun StringBuilder. join (words : Array <String >, left : Int , right : Int , separator : String ): StringBuilder {
45
+ append (words[left])
45
46
for (i in left + 1 .. < right) {
46
- builder. append(separator)
47
- builder. append(words[i])
47
+ append(separator)
48
+ append(words[i])
48
49
}
49
- return builder
50
+ return this
50
51
}
51
52
}
52
53
0 commit comments