Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5b9f346

Browse filesBrowse files
author
night
committed
opt code
1 parent 95fd471 commit 5b9f346
Copy full SHA for 5b9f346

File tree

1 file changed

+9
-8
lines changed
Filter options

1 file changed

+9
-8
lines changed

‎src/main/java/com/blankj/hard/_068/Solution.kt

Copy file name to clipboardExpand all lines: src/main/java/com/blankj/hard/_068/Solution.kt
+9-8Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blankj.hard._068
22

33
import com.blankj.ext.print
4+
import java.lang.Appendable
45

56
class Solution {
67
fun fullJustify(words: Array<String>, maxWidth: Int): List<String> {
@@ -14,7 +15,7 @@ class Solution {
1415
sumLength += words[right++].length
1516
}
1617
if (right == words.size) {
17-
val lastLine = join(words, left, words.size, " ")
18+
val lastLine = StringBuilder().join(words, left, words.size, " ")
1819
lastLine.append(" ".repeat(maxWidth - lastLine.length))
1920
res.add(lastLine.toString())
2021
return res
@@ -32,21 +33,21 @@ class Solution {
3233
val avgSpaces = numSpaces / (numWords - 1)
3334
val countWithextraSpaces = numSpaces % (numWords - 1)
3435
res.add(buildString {
35-
append(join(words, left, left + countWithextraSpaces + 1, " ".repeat(avgSpaces + 1)))
36+
join(words, left, left + countWithextraSpaces + 1, " ".repeat(avgSpaces + 1))
3637
append(" ".repeat(avgSpaces))
37-
append(join(words, left + countWithextraSpaces + 1, right, " ".repeat(avgSpaces)))
38+
join(words, left + countWithextraSpaces + 1, right, " ".repeat(avgSpaces))
3839
})
3940
}
4041
}
4142

4243
// 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])
4546
for (i in left + 1..<right) {
46-
builder.append(separator)
47-
builder.append(words[i])
47+
append(separator)
48+
append(words[i])
4849
}
49-
return builder
50+
return this
5051
}
5152
}
5253

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.