forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoobing2.ts
More file actions
42 lines (39 loc) · 783 Bytes
/
soobing2.ts
File metadata and controls
42 lines (39 loc) · 783 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
38
39
40
41
42
/**
* 문제 유형
* - String
*
* 문제 설명
* - 문자열 인코딩과 디코딩
*
* 아이디어
* 1) "길이 + # + 문자열" 형태로 인코딩
*
*/
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
return strs.map((str) => `${str.length}#${str}`).join("");
}
/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const result = [];
let tempStr = str;
while (tempStr.length) {
let i = 0;
while (tempStr[i] !== "#") {
i++;
}
const length = Number(tempStr.slice(0, i));
const currentStr = tempStr.slice(i + 1, i + 1 + length);
result.push(currentStr);
tempStr = tempStr.slice(length + i + 1);
}
return result;
}
}