当前位置:网站首页>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
边栏推荐
- 对p值的理解
- ShanDong Multi-University Training #3
- 二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
- 【综合案例】信用卡虚拟交易识别
- 这个EMR-SparkSQL节点,他查询的表是不是ODPS的啊?
- GBase8s数据库与 FOR UPDATE 子句不兼容的语法
- Gbase8s database select has order by Clause 6
- Students' programming stories
- AutoCAD - text display mode and how CAD can directly open Tianzheng drawings
- Pro test! Centos7 deploy PHP + spool
猜你喜欢

每周推荐短视频:爱因斯坦是怎样思考问题的?

Jericho's position on initiating the connection back to the opposite ear: 【 chapter 】

Interpolated scatter data

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

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

文件包含之日志中毒(User-Agent)

ERP编制物料清单 华夏
联想领像 lenovoimage 部分打印机 驱动 PPD 文件

Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions

智能垃圾桶(四)——树莓派pico实现超声波测距(HC-SR04)
随机推荐
& 4 express framework
MySQL master-slave synchronous asynchronous replication semi synchronous replication full synchronous replication
The blackened honeysnow ice city wants to grasp the hearts of consumers by marketing?
Gbase8s database select has order by Clause 1
After class assignment of module 5 of the construction practice camp
GBase8s数据库select有ORDER BY 子句3
谷粒商城项目
GBase8s数据库INTO EXTERNAL 子句
Is it safe for Hengtai securities to open an account? Ranking of securities
[pbootcms template] composition website / document download website source code
When you are young, you should be awake to fight, and when you are young, you should have the courage to try
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cycle functions [chapter]
Jerry's manual pairing method [chapter]
GBase8s数据库select有ORDER BY 子句5
Gbase8s database for read only clause
论文复现——AC-FPN:Attention-guided Context Feature Pyramid Network for Object Detection.
多项目开发入门-业务场景关联基础入门测试 工资表
东方财富证券开户安全吗 证券开户办理
GBase8s数据库INTO STANDARD 和 INTO RAW 子句
Introduction to multi project development - business scenario Association basic introduction test payroll