-
Notifications
You must be signed in to change notification settings - Fork 25
/
0535-encode-and-decode-tinyurl.js
61 lines (50 loc) · 1.4 KB
/
0535-encode-and-decode-tinyurl.js
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
51
52
53
54
55
56
57
58
59
60
61
// 535. Encode and Decode TinyURL
// Medium 74%
// Note: This is a companion problem to the System Design problem: Design
// TinyURL.
// TinyURL is a URL shortening service where you enter a URL such as
// https://leetcode.com/problems/design-tinyurl and it returns a short URL such
// as http://tinyurl.com/4e9iAk.
// Design the encode and decode methods for the TinyURL service. There is no
// restriction on how your encode/decode algorithm should work. You just need to
// ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded
// to the original URL.
const map = []
/**
* Encodes a URL to a shortened URL.
*
* @param {string} longUrl
* @return {string}
*/
const encode = function(longUrl) {
map.push(longUrl)
return 'http://tinyurl.com/' + (map.length - 1)
}
/**
* Decodes a shortened URL to its original URL.
*
* @param {string} shortUrl
* @return {string}
*/
const decode = function(shortUrl) {
const array = shortUrl.split('/')
const number = parseInt(array[array.length - 1])
return map[number]
}
/**
* Your functions will be called as such:
* decode(encode(url));
*/
;[
'https://leetcode.com/problems/design-tinyurl',
].forEach(s => {
console.log(s)
const short = encode(s)
console.log(short)
const long = decode(short)
console.log(long)
})
// Solution:
// 用数字来表示url,
// 并用数组来存储原来的url。
// Submission Result: Accepted