Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65b3267

Browse files
committed
feat: add solutions to leetcode problem: No.0535. Encode and Decode TinyURL
1 parent d059b3f commit 65b3267

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,72 @@
1010

1111
<p>要求:设计一个 TinyURL 的加密&nbsp;<code>encode</code>&nbsp;和解密&nbsp;<code>decode</code>&nbsp;的方法。你的加密和解密算法如何设计和运作是没有限制的,你只需要保证一个URL可以被加密成一个TinyURL,并且这个TinyURL可以用解密方法恢复成原本的URL。</p>
1212

13-
1413
## 解法
1514

1615
<!-- 这里可写通用的实现逻辑 -->
1716

17+
哈希表实现。
18+
1819
<!-- tabs:start -->
1920

2021
### **Python3**
2122

2223
<!-- 这里可写当前语言的特殊实现逻辑 -->
2324

2425
```python
25-
26+
class Codec:
27+
def __init__(self):
28+
self.code_url = {}
29+
self.count = 0
30+
self.prefix_url = 'http://tinyurl.com/'
31+
32+
def encode(self, longUrl: str) -> str:
33+
"""Encodes a URL to a shortened URL.
34+
"""
35+
self.count += 1
36+
code = str(hex(self.count))[2:]
37+
self.code_url[code] = longUrl
38+
return self.prefix_url + code
39+
40+
def decode(self, shortUrl: str) -> str:
41+
"""Decodes a shortened URL to its original URL.
42+
"""
43+
code = shortUrl.replace(self.prefix_url, '')
44+
return self.code_url[code]
45+
46+
# Your Codec object will be instantiated and called as such:
47+
# codec = Codec()
48+
# codec.decode(codec.encode(url))
2649
```
2750

2851
### **Java**
2952

3053
<!-- 这里可写当前语言的特殊实现逻辑 -->
3154

3255
```java
33-
56+
public class Codec {
57+
private Map<String, String> code2Url = new HashMap<>();
58+
private int count = 0;
59+
private static final String prefixUrl = "http://tinyurl.com/";
60+
61+
// Encodes a URL to a shortened URL.
62+
public String encode(String longUrl) {
63+
++count;
64+
String code = Integer.toHexString(count);
65+
code2Url.put(code, longUrl);
66+
return prefixUrl + code;
67+
}
68+
69+
// Decodes a shortened URL to its original URL.
70+
public String decode(String shortUrl) {
71+
String code = shortUrl.replace(prefixUrl, "");
72+
return code2Url.get(code);
73+
}
74+
}
75+
76+
// Your Codec object will be instantiated and called as such:
77+
// Codec codec = new Codec();
78+
// codec.decode(codec.encode(url));
3479
```
3580

3681
### **...**

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,68 @@
66

77
<blockquote>Note: This is a companion problem to the <a href="https://leetcode.com/discuss/interview-question/system-design/" target="_blank">System Design</a> problem: <a href="https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/" target="_blank">Design TinyURL</a>.</blockquote>
88

9-
10-
119
<p>TinyURL is a URL shortening service where you enter a URL such as <code>https://leetcode.com/problems/design-tinyurl</code> and it returns a short URL such as <code>http://tinyurl.com/4e9iAk</code>.</p>
1210

13-
14-
1511
<p>Design the <code>encode</code> and <code>decode</code> 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.</p>
1612

17-
18-
1913
## Solutions
2014

2115
<!-- tabs:start -->
2216

2317
### **Python3**
2418

2519
```python
26-
20+
class Codec:
21+
def __init__(self):
22+
self.code_url = {}
23+
self.count = 0
24+
self.prefix_url = 'http://tinyurl.com/'
25+
26+
def encode(self, longUrl: str) -> str:
27+
"""Encodes a URL to a shortened URL.
28+
"""
29+
self.count += 1
30+
code = str(hex(self.count))[2:]
31+
self.code_url[code] = longUrl
32+
return self.prefix_url + code
33+
34+
def decode(self, shortUrl: str) -> str:
35+
"""Decodes a shortened URL to its original URL.
36+
"""
37+
code = shortUrl.replace(self.prefix_url, '')
38+
return self.code_url[code]
39+
40+
# Your Codec object will be instantiated and called as such:
41+
# codec = Codec()
42+
# codec.decode(codec.encode(url))
2743
```
2844

2945
### **Java**
3046

3147
```java
32-
48+
public class Codec {
49+
private Map<String, String> code2Url = new HashMap<>();
50+
private int count = 0;
51+
private static final String prefixUrl = "http://tinyurl.com/";
52+
53+
// Encodes a URL to a shortened URL.
54+
public String encode(String longUrl) {
55+
++count;
56+
String code = Integer.toHexString(count);
57+
code2Url.put(code, longUrl);
58+
return prefixUrl + code;
59+
}
60+
61+
// Decodes a shortened URL to its original URL.
62+
public String decode(String shortUrl) {
63+
String code = shortUrl.replace(prefixUrl, "");
64+
return code2Url.get(code);
65+
}
66+
}
67+
68+
// Your Codec object will be instantiated and called as such:
69+
// Codec codec = new Codec();
70+
// codec.decode(codec.encode(url));
3371
```
3472

3573
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
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/";
5+
6+
// Encodes a URL to a shortened URL.
7+
public String encode(String longUrl) {
8+
++count;
9+
String code = Integer.toHexString(count);
10+
code2Url.put(code, longUrl);
11+
return prefixUrl + code;
12+
}
13+
14+
// Decodes a shortened URL to its original URL.
15+
public String decode(String shortUrl) {
16+
String code = shortUrl.replace(prefixUrl, "");
17+
return code2Url.get(code);
18+
}
19+
}
20+
21+
// Your Codec object will be instantiated and called as such:
22+
// Codec codec = new Codec();
23+
// codec.decode(codec.encode(url));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Codec:
2+
def __init__(self):
3+
self.code_url = {}
4+
self.count = 0
5+
self.prefix_url = 'http://tinyurl.com/'
6+
7+
def encode(self, longUrl: str) -> str:
8+
"""Encodes a URL to a shortened URL.
9+
"""
10+
self.count += 1
11+
code = str(hex(self.count))[2:]
12+
self.code_url[code] = longUrl
13+
return self.prefix_url + code
14+
15+
def decode(self, shortUrl: str) -> str:
16+
"""Decodes a shortened URL to its original URL.
17+
"""
18+
code = shortUrl.replace(self.prefix_url, '')
19+
return self.code_url[code]
20+
21+
# Your Codec object will be instantiated and called as such:
22+
# codec = Codec()
23+
# codec.decode(codec.encode(url))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /