forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleehyeyun.js
More file actions
51 lines (36 loc) · 1.07 KB
/
leehyeyun.js
File metadata and controls
51 lines (36 loc) · 1.07 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
39
40
41
42
43
44
45
46
47
48
49
50
var encode = function (strs) {
// strs: string[]
// return: string
// write your code here
let encode_result = "";
for (const encode_strs of strs)
{
const encode_strs_length = encode_strs.length;
if(encode_result != undefined)
{
encode_result = encode_result + encode_strs_length + "|" + encode_strs;
}
}
return encode_result;
};
var decode = function (str) {
// str: string
// return: string[]
// write your code here
let results = [];
while (str.length > 0) {
let i = 0;
while (str[i] >= '0' && str[i] <= '9') {
i++;
}
let decode_str_length = str.substring(0, i);
let len = parseInt(decode_str_length, 10);
let newStr = str.substring(i + 1);
let result = newStr.substr(0,decode_str_length)
str = newStr.substring(len);
results.push(result);
}
return results;
};
console.log(decode(encode(["lint","code","love","you"])));
console.log(decode(encode(["we","say",":","yes"])));