forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanniemon.js
More file actions
37 lines (36 loc) ยท 940 Bytes
/
anniemon.js
File metadata and controls
37 lines (36 loc) ยท 940 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
35
36
37
class Solution {
/**
* ์๊ฐ ๋ณต์ก๋: strs์ ๊ธธ์ด๋งํผ ์ํํ๋ฏ๋ก, O(n)
* ๊ณต๊ฐ ๋ณต์ก๋: ๊ฒฐ๊ด๊ฐ ์ ์ธ ์ถ๊ฐ ๋ณ์ ์ฌ์ฉ ์์ผ๋ฏ๋ก O(1)
*/
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return strs.reduce((acc, cur) => acc+ `${cur.length}` + '!' + cur, '');
}
/**
* ์๊ฐ ๋ณต์ก๋: str์ ๊ธธ์ด๋งํผ ์ํํ๋ฏ๋ก str์ ๊ธธ์ด๊ฐ m์ด๋ฉด, O(m)
* ๊ณต๊ฐ ๋ณต์ก๋: ๊ฒฐ๊ด๊ฐ ์ ์ธ ์์ ํฌ๊ธฐ ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก, O(1)
*/
/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const res = [];
let i = 0;
while (i < str.length) {
let j = i;
while (str[j] !== '!') {
j++;
}
const len = Number(str.slice(i, j));
const s = str.slice(j + 1, j + 1 + len);
res.push(s);
i = j + 1 + len;
}
return res;
}
}