Skip to content

Commit 853207c

Browse files
author
chenzhao
committed
mod
1 parent 47adaee commit 853207c

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

0880. Decoded String at Index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,38 @@ const decodeAtIndex = (S, K) => {
5757
return res[K - 1]
5858
}
5959

60+
61+
// 2)
62+
// N:遍历编码字符串 S,获取大于 K 位置的解码长度
63+
// i:遍历的次数,反向递减 i
64+
// 如果 s = S[i] 是数字,那么 N / d 是重复的部分,K % N 是最终的字符
65+
// 如果 s = S[i] 是字符,如果 K % N === 0,返回 s
66+
/**
67+
* @param {string} S
68+
* @param {number} K
69+
* @return {string}
70+
*/
71+
const decodeAtIndex = (S, K) => {
72+
let N = 0
73+
let i = 0
74+
for (; N < K; i++) {
75+
let s = S[i]
76+
if (String(Number(s)).length > 1) {
77+
N += 1
78+
} else {
79+
N *= Number(s)
80+
}
81+
}
82+
while (i--) {
83+
let s = S[i]
84+
if (String(Number(s)).length === 1) {
85+
N /= Number(s)
86+
K %= N
87+
} else if (K % N-- === 0) {
88+
return s
89+
}
90+
}
91+
}
92+
// Runtime: 52 ms, faster than 78.13% of JavaScript online submissions for Decoded String at Index.
93+
// Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions for Decoded String at Index.
94+

0 commit comments

Comments
 (0)