Skip to content

Commit 61cad09

Browse files
committed
https://leetcode.cn/problems/split-message-based-on-limit
1 parent f3cbd73 commit 61cad09

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/split-message-based-on-limit
49+
4850
https://leetcode.cn/problems/count-ways-to-build-good-string
4951

5052
https://leetcode.cn/problems/number-of-distinct-averages
@@ -129,8 +131,6 @@ https://leetcode.cn/problems/magical-string/
129131

130132
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/
131133

132-
https://leetcode.cn/problems/split-message-based-on-limit
133-
134134
https://leetcode.cn/problems/nge-tou-zi-de-dian-shu-lcof
135135

136136
https://leetcode.cn/problems/count-of-matches-in-tournament/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
val endIndex = Math.max(
2525
0, Math.min(message.length, len + limit - 3 - (it + 1).toString().length - left.toString().length)
2626
)
27-
// println("${it},${len},${endIndex}")
27+
2828
val s = message.substring(len, endIndex) + "<${it + 1}/${left}>";
2929
if (len == endIndex) return arrayOf()
3030
len = endIndex

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default function splitMessage(message: string, limit: number): string[] {
2+
const n = message.length;
3+
for (let i = 1, cap = 0, tail_len = 0;; ++i) {
4+
if (i < 10) tail_len = 5; // 结尾的长度
5+
else if (i < 100) {
6+
if (i == 10) cap -= 9; // 前面的结尾的长度都 +1,那么容量就要减小
7+
tail_len = 7;
8+
} else if (i < 1000) {
9+
if (i == 100) cap -= 99;
10+
tail_len = 9;
11+
} else {
12+
if (i == 1000) cap -= 999;
13+
tail_len = 11;
14+
}
15+
if (tail_len >= limit) return []; // cap 无法增大,寄
16+
cap += limit - tail_len;
17+
if (cap < n) continue; // 容量没有达到,继续枚举
18+
19+
const ans = Array<string>(i).fill("");
20+
for (let j = 0, k = 0; j < i; ++j) {
21+
const tail = "<" + (j + 1) + "/" + i + ">";
22+
if (j == i - 1) ans[j] = message.substring(k) + tail;
23+
else {
24+
const m = limit - tail.length;
25+
ans[j] = message.substring(k, k + m) + tail;
26+
k += m;
27+
}
28+
}
29+
return ans;
30+
}
31+
}

0 commit comments

Comments
 (0)