File tree Expand file tree Collapse file tree 3 files changed +34
-3
lines changed
split-message-based-on-limit Expand file tree Collapse file tree 3 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
48
+ https://leetcode.cn/problems/split-message-based-on-limit
49
+
48
50
https://leetcode.cn/problems/count-ways-to-build-good-string
49
51
50
52
https://leetcode.cn/problems/number-of-distinct-averages
@@ -129,8 +131,6 @@ https://leetcode.cn/problems/magical-string/
129
131
130
132
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/
131
133
132
- https://leetcode.cn/problems/split-message-based-on-limit
133
-
134
134
https://leetcode.cn/problems/nge-tou-zi-de-dian-shu-lcof
135
135
136
136
https://leetcode.cn/problems/count-of-matches-in-tournament/
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ class Solution {
24
24
val endIndex = Math .max(
25
25
0 , Math .min(message.length, len + limit - 3 - (it + 1 ).toString().length - left.toString().length)
26
26
)
27
- // println("${it},${len},${endIndex}")
27
+
28
28
val s = message.substring(len, endIndex) + " <${it + 1 } /${left} >" ;
29
29
if (len == endIndex) return arrayOf()
30
30
len = endIndex
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments