当前位置:网站首页>535. encryption and decryption of tinyurl: design a URL simplification system
535. encryption and decryption of tinyurl: design a URL simplification system
2022-06-29 12:27:00 【Gong Shui Sanye's Diary】
Title Description
This is a LeetCode Upper 「535. TinyURL Encryption and decryption 」 , The difficulty is 「 secondary 」.
Tag : 「 Hashtable 」、「 simulation 」
TinyURL It's a kind of URL Simplify services , such as : When you enter a URL https://leetcode.com/problems/design-tinyurl when , It will return a simplified URL http://tinyurl.com/4e9iAk .
Please design a class to encrypt and decrypt TinyURL .
There is no limit to how encryption and decryption algorithms are designed and operated , You just need to guarantee one URL Can be encrypted into a TinyURL , And this TinyURL It can be restored to the original by decryption URL .
Realization Solution class :
Solution()initializationTinyURLSystem object .String encode(String longUrl)returnlongUrlCorrespondingTinyURL.String decode(String shortUrl)returnshortUrlThe originalURL. The subject data guarantees a givenshortUrlIs encrypted by the same system object .
Example :
Input :url = "https://leetcode.com/problems/design-tinyurl"
Output :"https://leetcode.com/problems/design-tinyurl"
explain :
Solution obj = new Solution();
string tiny = obj.encode(url); // Returns the result of encryption TinyURL .
string ans = obj.decode(tiny); // Returns the original after decryption URL .
Tips :
Topic data assurance urlIs an effectiveURL
simulation + Hashtable
For each longUrl We are all here. 「 Capital / Lowercase letters / Numbers 」 Medium random Bit as its mapping identifier , This requires the use of a hash table tiny2Origin For recording .
It also prevents the same longUrl Multiple calls , Make sure encode Service 「 Idempotency 」, Let's create an additional hash table origin2Tiny To record the correspondence between the original string and the mapping ID .
Code :
public class Codec {
Map<String, String> origin2Tiny = new HashMap<>(), tiny2Origin = new HashMap<>();
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
String prefix = "https://acoier.com/tags/";
int k = 6;
Random random = new Random();
public String encode(String longUrl) {
while (!origin2Tiny.containsKey(longUrl)) {
char[] cs = new char[k];
for (int i = 0; i < k; i++) cs[i] = str.charAt(random.nextInt(str.length()));
String cur = prefix + String.valueOf(cs);
if (tiny2Origin.containsKey(cur)) continue;
tiny2Origin.put(cur, longUrl);
origin2Tiny.put(longUrl, cur);
}
return origin2Tiny.get(longUrl);
}
public String decode(String shortUrl) {
return tiny2Origin.get(shortUrl);
}
}
Time complexity : encodeThe operation complexity is , among Is the short string length , Pass in parameters forlongUrlThe length of ( Storing in the hash table requires calculationlongUrlHash value , This process needs to traverselongUrl);decodeThe operation complexity is , among Pass in parameters forshortUrlThe length of , This length is fixed toprefixThe length of the addSpatial complexity : , among Is the number of calls , Is the average longUrllength
Last
This is us. 「 Brush through LeetCode」 The first of the series No.535 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
More, more, more popular 「 written examination / interview 」 Relevant information can be found in the beautifully arranged Gather new bases
边栏推荐
- 模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!
- What are outer chain and inner chain?
- Ttchat x Zadig open source co creates helm access scenarios, and environmental governance can be done!
- Li Kou daily question - day 31 -13 Roman array to integer
- torch.load加载模型报错:Can‘t get attribute ‘vae_vc‘ on <module ‘__main__‘ from ‘xxxx()运行文件路径‘
- 求大数的阶乘 ← C语言
- 缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
- ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集
- GBase8s数据库在组合查询中的集合运算符
- 大家有没有觉得学机械的人很可怕?
猜你喜欢
![[JUC series] ThreadLocal of synchronization tool class](/img/15/2f8ce68b9e5ee8dab03fb688712935.png)
[JUC series] ThreadLocal of synchronization tool class

大家有没有觉得学机械的人很可怕?

Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released

Ttchat x Zadig open source co creates helm access scenarios, and environmental governance can be done!

ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集

Go高级工程师必修课 | 真心建议你来听听,别错过~

Interview shock 61: tell me about MySQL transaction isolation level?

Sofaregistry source code | data synchronization module analysis
![Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cycle functions [chapter]](/img/d7/f73e748ada302440326a8b1a46f916.png)
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cycle functions [chapter]

如何查看网站已经保存的密码
随机推荐
Artbench: the first class balanced, high-quality, clean annotated and standardized artwork generation data set
对p值的理解
Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
论文复现——AC-FPN:Attention-guided Context Feature Pyramid Network for Object Detection.
MIT线性代数中文笔记
GBase8s数据库INTO table 子句
参加2022年杭州站Cocos Star Meetings
Jericho's position on initiating the connection back to the opposite ear: 【 chapter 】
torch.load加载模型报错:Can‘t get attribute ‘vae_vc‘ on <module ‘__main__‘ from ‘xxxx()运行文件路径‘
内插散点数据
ArtBench:第一个类平衡的、高质量的、干净注释的和标准化的艺术品生成数据集
现在怎么开户?有没有更快又安全的开通渠道
智能垃圾桶(四)——树莓派pico实现超声波测距(HC-SR04)
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
GBase8s数据库FOR READ ONLY 子句
力扣每日一题-第31天-13.三角形的最大周长
What are outer chain and inner chain?
[JUC series] ThreadLocal of synchronization tool class
Wonderful! Miaoying technology fully implements Zadig to help container construction, and fully embraces kubernetes and Yunyuan
Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released