Skip to content

Commit 0c64f10

Browse files
committed
feat: add solutions to lc problem: No.0535
No.0535.Encode and Decode TinyURL
1 parent b014c09 commit 0c64f10

File tree

6 files changed

+240
-54
lines changed

6 files changed

+240
-54
lines changed

solution/0500-0599/0535.Encode and Decode TinyURL/README.md

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
哈希表实现。
52+
**方法一:哈希表**
5353

5454
<!-- tabs:start -->
5555

@@ -60,23 +60,23 @@ string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
6060
```python
6161
class Codec:
6262
def __init__(self):
63-
self.code_url = {}
64-
self.count = 0
65-
self.prefix_url = 'http://tinyurl.com/'
63+
self.m = defaultdict()
64+
self.idx = 0
65+
self.domain = 'https://tinyurl.com/'
6666

6767
def encode(self, longUrl: str) -> str:
6868
"""Encodes a URL to a shortened URL.
6969
"""
70-
self.count += 1
71-
code = str(hex(self.count))[2:]
72-
self.code_url[code] = longUrl
73-
return self.prefix_url + code
70+
self.idx += 1
71+
self.m[str(self.idx)] = longUrl
72+
return f'{self.domain}{self.idx}'
7473

7574
def decode(self, shortUrl: str) -> str:
7675
"""Decodes a shortened URL to its original URL.
7776
"""
78-
code = shortUrl.replace(self.prefix_url, '')
79-
return self.code_url[code]
77+
idx = shortUrl.split('/')[-1]
78+
return self.m[idx]
79+
8080

8181
# Your Codec object will be instantiated and called as such:
8282
# codec = Codec()
@@ -89,21 +89,21 @@ class Codec:
8989

9090
```java
9191
public class Codec {
92-
private Map<String, String> code2Url = new HashMap<>();
93-
private int count = 0;
94-
private static final String prefixUrl = "http://tinyurl.com/";
92+
private Map<String, String> m = new HashMap<>();
93+
private int idx = 0;
94+
private String domain = "https://tinyurl.com/";
9595

9696
// Encodes a URL to a shortened URL.
9797
public String encode(String longUrl) {
98-
String code = Integer.toHexString(++count);
99-
code2Url.put(code, longUrl);
100-
return prefixUrl + code;
98+
String v = String.valueOf(++idx);
99+
m.put(v, longUrl);
100+
return domain + v;
101101
}
102102

103103
// Decodes a shortened URL to its original URL.
104104
public String decode(String shortUrl) {
105-
String code = shortUrl.replace(prefixUrl, "");
106-
return code2Url.get(code);
105+
int i = shortUrl.lastIndexOf('/') + 1;
106+
return m.get(shortUrl.substring(i));
107107
}
108108
}
109109

@@ -112,6 +112,71 @@ public class Codec {
112112
// codec.decode(codec.encode(url));
113113
```
114114

115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
121+
// Encodes a URL to a shortened URL.
122+
string encode(string longUrl) {
123+
string v = to_string(++idx);
124+
m[v] = longUrl;
125+
return domain + v;
126+
}
127+
128+
// Decodes a shortened URL to its original URL.
129+
string decode(string shortUrl) {
130+
int i = shortUrl.rfind('/') + 1;
131+
return m[shortUrl.substr(i, shortUrl.size() - i)];
132+
}
133+
134+
private:
135+
unordered_map<string, string> m;
136+
int idx = 0;
137+
string domain = "https://tinyurl.com/";
138+
};
139+
140+
// Your Solution object will be instantiated and called as such:
141+
// Solution solution;
142+
// solution.decode(solution.encode(url));
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
type Codec struct {
149+
m map[int]string
150+
idx int
151+
}
152+
153+
func Constructor() Codec {
154+
m := map[int]string{}
155+
return Codec{m, 0}
156+
}
157+
158+
// Encodes a URL to a shortened URL.
159+
func (this *Codec) encode(longUrl string) string {
160+
this.idx++
161+
this.m[this.idx] = longUrl
162+
return "https://tinyurl.com/" + strconv.Itoa(this.idx)
163+
}
164+
165+
// Decodes a shortened URL to its original URL.
166+
func (this *Codec) decode(shortUrl string) string {
167+
i := strings.LastIndexByte(shortUrl, '/')
168+
v, _ := strconv.Atoi(shortUrl[i+1:])
169+
return this.m[v]
170+
}
171+
172+
/**
173+
* Your Codec object will be instantiated and called as such:
174+
* obj := Constructor();
175+
* url := obj.encode(longUrl);
176+
* ans := obj.decode(url);
177+
*/
178+
```
179+
115180
### **...**
116181

117182
```

solution/0500-0599/0535.Encode and Decode TinyURL/README_EN.md

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ string ans = obj.decode(tiny); // returns the original url after deconding it.
4848
```python
4949
class Codec:
5050
def __init__(self):
51-
self.code_url = {}
52-
self.count = 0
53-
self.prefix_url = 'http://tinyurl.com/'
51+
self.m = defaultdict()
52+
self.idx = 0
53+
self.domain = 'https://tinyurl.com/'
5454

5555
def encode(self, longUrl: str) -> str:
5656
"""Encodes a URL to a shortened URL.
5757
"""
58-
self.count += 1
59-
code = str(hex(self.count))[2:]
60-
self.code_url[code] = longUrl
61-
return self.prefix_url + code
58+
self.idx += 1
59+
self.m[str(self.idx)] = longUrl
60+
return f'{self.domain}{self.idx}'
6261

6362
def decode(self, shortUrl: str) -> str:
6463
"""Decodes a shortened URL to its original URL.
6564
"""
66-
code = shortUrl.replace(self.prefix_url, '')
67-
return self.code_url[code]
65+
idx = shortUrl.split('/')[-1]
66+
return self.m[idx]
67+
6868

6969
# Your Codec object will be instantiated and called as such:
7070
# codec = Codec()
@@ -75,21 +75,21 @@ class Codec:
7575

7676
```java
7777
public class Codec {
78-
private Map<String, String> code2Url = new HashMap<>();
79-
private int count = 0;
80-
private static final String prefixUrl = "http://tinyurl.com/";
78+
private Map<String, String> m = new HashMap<>();
79+
private int idx = 0;
80+
private String domain = "https://tinyurl.com/";
8181

8282
// Encodes a URL to a shortened URL.
8383
public String encode(String longUrl) {
84-
String code = Integer.toHexString(++count);
85-
code2Url.put(code, longUrl);
86-
return prefixUrl + code;
84+
String v = String.valueOf(++idx);
85+
m.put(v, longUrl);
86+
return domain + v;
8787
}
8888

8989
// Decodes a shortened URL to its original URL.
9090
public String decode(String shortUrl) {
91-
String code = shortUrl.replace(prefixUrl, "");
92-
return code2Url.get(code);
91+
int i = shortUrl.lastIndexOf('/') + 1;
92+
return m.get(shortUrl.substring(i));
9393
}
9494
}
9595

@@ -98,6 +98,71 @@ public class Codec {
9898
// codec.decode(codec.encode(url));
9999
```
100100

101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
107+
// Encodes a URL to a shortened URL.
108+
string encode(string longUrl) {
109+
string v = to_string(++idx);
110+
m[v] = longUrl;
111+
return domain + v;
112+
}
113+
114+
// Decodes a shortened URL to its original URL.
115+
string decode(string shortUrl) {
116+
int i = shortUrl.rfind('/') + 1;
117+
return m[shortUrl.substr(i, shortUrl.size() - i)];
118+
}
119+
120+
private:
121+
unordered_map<string, string> m;
122+
int idx = 0;
123+
string domain = "https://tinyurl.com/";
124+
};
125+
126+
// Your Solution object will be instantiated and called as such:
127+
// Solution solution;
128+
// solution.decode(solution.encode(url));
129+
```
130+
131+
### **Go**
132+
133+
```go
134+
type Codec struct {
135+
m map[int]string
136+
idx int
137+
}
138+
139+
func Constructor() Codec {
140+
m := map[int]string{}
141+
return Codec{m, 0}
142+
}
143+
144+
// Encodes a URL to a shortened URL.
145+
func (this *Codec) encode(longUrl string) string {
146+
this.idx++
147+
this.m[this.idx] = longUrl
148+
return "https://tinyurl.com/" + strconv.Itoa(this.idx)
149+
}
150+
151+
// Decodes a shortened URL to its original URL.
152+
func (this *Codec) decode(shortUrl string) string {
153+
i := strings.LastIndexByte(shortUrl, '/')
154+
v, _ := strconv.Atoi(shortUrl[i+1:])
155+
return this.m[v]
156+
}
157+
158+
/**
159+
* Your Codec object will be instantiated and called as such:
160+
* obj := Constructor();
161+
* url := obj.encode(longUrl);
162+
* ans := obj.decode(url);
163+
*/
164+
```
165+
101166
### **...**
102167

103168
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
4+
// Encodes a URL to a shortened URL.
5+
string encode(string longUrl) {
6+
string v = to_string(++idx);
7+
m[v] = longUrl;
8+
return domain + v;
9+
}
10+
11+
// Decodes a shortened URL to its original URL.
12+
string decode(string shortUrl) {
13+
int i = shortUrl.rfind('/') + 1;
14+
return m[shortUrl.substr(i, shortUrl.size() - i)];
15+
}
16+
17+
private:
18+
unordered_map<string, string> m;
19+
int idx = 0;
20+
string domain = "https://tinyurl.com/";
21+
};
22+
23+
// Your Solution object will be instantiated and called as such:
24+
// Solution solution;
25+
// solution.decode(solution.encode(url));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
type Codec struct {
2+
m map[int]string
3+
idx int
4+
}
5+
6+
func Constructor() Codec {
7+
m := map[int]string{}
8+
return Codec{m, 0}
9+
}
10+
11+
// Encodes a URL to a shortened URL.
12+
func (this *Codec) encode(longUrl string) string {
13+
this.idx++
14+
this.m[this.idx] = longUrl
15+
return "https://tinyurl.com/" + strconv.Itoa(this.idx)
16+
}
17+
18+
// Decodes a shortened URL to its original URL.
19+
func (this *Codec) decode(shortUrl string) string {
20+
i := strings.LastIndexByte(shortUrl, '/')
21+
v, _ := strconv.Atoi(shortUrl[i+1:])
22+
return this.m[v]
23+
}
24+
25+
/**
26+
* Your Codec object will be instantiated and called as such:
27+
* obj := Constructor();
28+
* url := obj.encode(longUrl);
29+
* ans := obj.decode(url);
30+
*/

solution/0500-0599/0535.Encode and Decode TinyURL/Solution.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
public class Codec {
2-
private Map<String, String> code2Url = new HashMap<>();
3-
private int count = 0;
4-
private static final String prefixUrl = "http://tinyurl.com/";
2+
private Map<String, String> m = new HashMap<>();
3+
private int idx = 0;
4+
private String domain = "https://tinyurl.com/";
55

66
// Encodes a URL to a shortened URL.
77
public String encode(String longUrl) {
8-
String code = Integer.toHexString(++count);
9-
code2Url.put(code, longUrl);
10-
return prefixUrl + code;
8+
String v = String.valueOf(++idx);
9+
m.put(v, longUrl);
10+
return domain + v;
1111
}
1212

1313
// Decodes a shortened URL to its original URL.
1414
public String decode(String shortUrl) {
15-
String code = shortUrl.replace(prefixUrl, "");
16-
return code2Url.get(code);
15+
int i = shortUrl.lastIndexOf('/') + 1;
16+
return m.get(shortUrl.substring(i));
1717
}
1818
}
1919

solution/0500-0599/0535.Encode and Decode TinyURL/Solution.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
class Codec:
22
def __init__(self):
3-
self.code_url = {}
4-
self.count = 0
5-
self.prefix_url = 'http://tinyurl.com/'
3+
self.m = defaultdict()
4+
self.idx = 0
5+
self.domain = 'https://tinyurl.com/'
66

77
def encode(self, longUrl: str) -> str:
8-
"""Encodes a URL to a shortened URL."""
9-
self.count += 1
10-
code = str(hex(self.count))[2:]
11-
self.code_url[code] = longUrl
12-
return self.prefix_url + code
8+
"""Encodes a URL to a shortened URL.
9+
"""
10+
self.idx += 1
11+
self.m[str(self.idx)] = longUrl
12+
return f'{self.domain}{self.idx}'
1313

1414
def decode(self, shortUrl: str) -> str:
15-
"""Decodes a shortened URL to its original URL."""
16-
code = shortUrl.replace(self.prefix_url, '')
17-
return self.code_url[code]
15+
"""Decodes a shortened URL to its original URL.
16+
"""
17+
idx = shortUrl.split('/')[-1]
18+
return self.m[idx]
1819

1920

2021
# Your Codec object will be instantiated and called as such:

0 commit comments

Comments
 (0)