We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 4db9b7e + 2393936 commit 4eddd55Copy full SHA for 4eddd55
javascript/0535-encode-and-decode-tinyurl.js
@@ -1,22 +1,18 @@
1
// problem link https://leetcode.com/problems/encode-and-decode-tinyurl
2
// time complexity O(1)
3
4
-
5
const encodeMap = new Map();
6
const decodeMap = new Map();
7
const base = 'http://tinyurl.com/';
8
9
var encode = function(longUrl) {
10
let shortUrl = ''
11
if(!encodeMap.has(longUrl)) {
12
- shortUrl = (base + encodeMap.size + 1).toString();
+ shortUrl = base + encodeMap.size + 1
13
encodeMap.set(longUrl, shortUrl);
14
decodeMap.set(shortUrl, longUrl);
15
- } else {
16
- return encodeMap.has(longUrl);
17
- }
18
19
- return shortUrl;
+ }
+ return shortUrl || encodeMap.get(longUrl);
20
};
21
22
var decode = function(shortUrl) {
0 commit comments