Skip to content

Commit 8c9d7fd

Browse files
committed
测试
1 parent 61cad09 commit 8c9d7fd

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

split-message-based-on-limit/index.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
package com.github.masx200.split_message_based_on_limit
1+
package com.github.masx200.leetcode_test.split_message_based_on_limit
22

33
class Solution {
44
fun splitMessage(message: String, limit: Int): Array<String> {
55
if (limit <= 5) return arrayOf()
6+
val results = splitStrings(message, limit, 0, 10)
7+
if (results.isNotEmpty()) return results
68

9+
val results2 = splitStrings(message, limit, 10, message.length)
10+
if (results2.isNotEmpty()) return results2
11+
12+
return arrayOf()
13+
}
14+
15+
private fun splitStrings(message: String, limit: Int, lleft: Int, rright: Int): Array<String> {
16+
var left = lleft
17+
var right = rright
718

8-
var left = 0
9-
var right = message.length
1019

1120
while (left < right) {
1221
val mid = (left + right) / 2
@@ -16,7 +25,8 @@ class Solution {
1625
left = mid + 1
1726
}
1827
}
19-
if (left > 0 && left < message.length + 1) {
28+
29+
if (left > 0 && left < rright) {
2030

2131
var len = 0
2232
return Array(left) {
@@ -26,6 +36,7 @@ class Solution {
2636
)
2737

2838
val s = message.substring(len, endIndex) + "<${it + 1}/${left}>";
39+
2940
if (len == endIndex) return arrayOf()
3041
len = endIndex
3142
s
@@ -46,4 +57,4 @@ private fun checkMessage(message: String, limit: Int, count: Int): Boolean {
4657
len += limit - (3 + i.toString().length + count.toString().length)
4758
}
4859
return len >= message.length
49-
}
60+
}

test/com/github/masx200/split_message_based_on_limit/SolutionTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.masx200.split_message_based_on_limit
1+
package com.github.masx200.leetcode_test.split_message_based_on_limit
22

33
import org.junit.jupiter.api.Test
44
import kotlin.test.assertContentEquals
@@ -41,6 +41,12 @@ internal class SolutionTest {
4141
Solution().splitMessage(
4242
"short message", 15
4343
)
44+
);
45+
assertContentEquals(
46+
arrayOf("abc<1/7>", "dab<2/7>", "cda<3/7>", "bcd<4/7>", "abc<5/7>", "dab<6/7>", "cd<7/7>"),
47+
Solution().splitMessage(
48+
"abcdabcdabcdabcdabcd", 8
49+
)
4450
)
4551
}
4652
}

0 commit comments

Comments
 (0)