forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkrokerdile.js
More file actions
35 lines (31 loc) · 703 Bytes
/
krokerdile.js
File metadata and controls
35 lines (31 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
let result = "";
for (const str of strs) {
result += `${str.length}#${str}`;
}
return result;
}
/**
* @param {string} str
* @returns {string[]}
*/
decode(s) {
let result = [];
let i = 0;
// 5#hello5#world
while (i < s.length) {
const pos = s.indexOf("#", i);
const len = parseInt(s.slice(i, pos));// 5
i = pos + 1;
const str = s.slice(i, i + len);
result.push(str);
i += len;
}
return result;
}
}