forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclara-shin.js
More file actions
38 lines (32 loc) ยท 1.22 KB
/
clara-shin.js
File metadata and controls
38 lines (32 loc) ยท 1.22 KB
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
/**
* ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๊ณ , ๋ค์ ๋์ฝ๋ฉ ํ๊ธฐ
*
* ์ ๊ทผ ๋ฐฉ๋ฒ:
* ๊ธธ์ด ๊ธฐ๋ฐ ์ธ์ฝ๋ฉ => ๊ฐ ๋ฌธ์์ด๋ง๋ค "๊ธธ์ด#๋ฌธ์์ด" ํ์์ผ๋ก ์ธ์ฝ๋ฉ
* "#"์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋๋๊ณ , ๊ธธ์ด๋ฅผ ์ด์ฉํด ์๋ ๋ฌธ์์ด์ ๋ณต์ํ์ฌ ๋์ฝ๋ฉ
*/
/**
* @param {string[]} strs - ์ธ์ฝ๋ฉํ ๋ฌธ์์ด ๋ฆฌ์คํธ
* @return {string} - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
*/
var encode = function (strs) {
if (!strs || strs.length === 0) return '';
return strs.map((str) => str.length + '#' + str).join('');
};
/**
* @param {string} s - ๋์ฝ๋ฉํ ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
* @return {string[]} - ์๋์ ๋ฌธ์์ด ๋ฆฌ์คํธ
*/
var decode = function (s) {
if (!s || s.length === 0) return [];
const result = [];
let i = 0;
while (i < s.length) {
const delimiterIndex = s.indexOf('#', i); // '#'์ ์์น ์ฐพ๊ธฐ
const length = parseInt(s.slice(i, delimiterIndex)); // ๊ธธ์ด ์ถ์ถ
const start = delimiterIndex + 1; // ๋ฌธ์์ด ์์ ์์น
result.push(s.slice(start, start + length)); // ์์๋ธ ๊ธธ์ด๋งํผ ๋ฌธ์์ด ์ถ์ถ ํ ๊ฒฐ๊ณผ์ ์ถ๊ฐ
i = start + length; // ๋ค์ ๋ฌธ์์ด ์์ ์์น๋ก ํฌ์ธํฐ ์ด๋
}
return result;
};