当前位置:网站首页>[535. encryption and decryption of tinyurl]
[535. encryption and decryption of tinyurl]
2022-06-30 01:15:00 【Sugar_ wolf】
source : Power button (LeetCode)
describe :
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()initialization TinyURL System object .String encode(String longUrl)returnlongUrlCorresponding TinyURL .String decode(String shortUrl)return shortUrl The original URL . The subject data guarantees a given shortUrl Is 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 :
- 1 <= url.length <= 104
- Topic data assurance url Is an effective URL
Preface
The title does not require the same URL Must be encrypted to the same TinyURL, Therefore, the method in this paper does not meet the same URL Encrypt to the same TinyURL. If you want to achieve the same URL Encrypt to the same TinyURL, Save an additional from URL To TinyURL Mapping .
Method 1 : Self increasing
Encode function
Use self increasing id As longUrl Key , Every time I receive one longUrl All will id Add one , Set the key value to (id,longUrl) Insert database dataBase, Then return with \textit{id}id As a string of shorUrl.
Decode function
take shortUrl Convert to corresponding }key, Then in the database dataBase Search for key Corresponding longUrl.
Code :
class Solution {
private:
unordered_map<int, string> dataBase;
int id;
public:
Solution() {
id = 0;
}
string encode(string longUrl) {
id++;
dataBase[id] = longUrl;
return string("http://tinyurl.com/") + to_string(id);
}
string decode(string shortUrl) {
int p = shortUrl.rfind('/') + 1;
int key = stoi(shortUrl.substr(p, int(shortUrl.size()) - p));
return dataBase[key];
}
};
Execution time :4 ms, In all C++ Defeated in submission 70.90% Users of
Memory consumption :6.8 MB, In all C++ Defeated in submission 36.81% Users of
Complexity analysis
Time complexity :
Encode function : O(n), among n Is string longUrl The length of .
Decode function : O(1). We should shortUrl Treat as a finite length string .
Spatial complexity :
Encode function : O(n). Save string longUrl need O(n) Space .
Decode function : O(1).
Method 2 : Hash generation 
Code :
const long long k1 = 1117;
const long long k2 = 1e9 + 7;
class Solution {
private:
unordered_map<int, string> dataBase;
unordered_map<string, int> urlToKey;
public:
Solution() {
}
string encode(string longUrl) {
if (urlToKey.count(longUrl) > 0) {
return string("http://tinyurl.com/") + to_string(urlToKey[longUrl]);
}
long long key = 0, base = 1;
for (auto c : longUrl) {
key = (key + c * base) % k2;
base = (base * k1) % k2;
}
while (dataBase.count(key) > 0) {
key = (key + 1) % k2;
}
dataBase[key] = longUrl;
urlToKey[longUrl] = key;
return string("http://tinyurl.com/") + to_string(key);
}
string decode(string shortUrl) {
int p = shortUrl.rfind('/') + 1;
int key = stoi(shortUrl.substr(p, int(shortUrl.size()) - p));
return dataBase[key];
}
};
Execution time :0 ms, In all C++ Defeated in submission 100.00% Users of
Memory consumption :7 MB, In all C++ Defeated in submission 10.69% Users of
Method 3 : Random generation 
Code :
class Solution {
private:
unordered_map<int, string> dataBase;
public:
Solution() {
srand(time(0));
}
string encode(string longUrl) {
int key;
while (true) {
key = rand();
if (dataBase.count(key) == 0) {
break;
}
}
dataBase[key] = longUrl;
return string("http://tinyurl.com/") + to_string(key);
}
string decode(string shortUrl) {
int p = shortUrl.rfind('/') + 1;
int key = stoi(shortUrl.substr(p, int(shortUrl.size()) - p));
return dataBase[key];
}
};
Execution time :4 ms, In all C++ Defeated in submission 70.90% Users of
Memory consumption :6.8 MB, In all C++ Defeated in submission 31.09% Users of
author:LeetCode-Solution
边栏推荐
- Tetris game based on STM32F103
- 2022年最新最详细IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
- I learned database at station B (V): DQL exercise
- Cantilever beam calculation [matlab code]
- Solve the problem of repairing Visual Basic exceptions with excel/wps plug-in of choice financial terminal
- 【Proteus仿真】8位端口检测8独立按键
- Live broadcast configuration of crmeb knowledge payment system program configuration (method 2)
- Simple pages
- Using tsne to visualize the similarity of different sentences
- 挖财的课程靠谱吗,让开户安全吗?
猜你喜欢

在线SQL转CSV工具

Analysis of IM instant messaging development technology on modern web

Stimulsoft Reports报告工具,Stimulsoft创建和构建报告

Quality management of functional modules of MES management system

Good test / development programmers vs. average programmers

【深度学习编译】算子编译 IR 转换

Programmers with a monthly salary of less than 30K must recite the interview stereotype. I will eat it first!

Wechat applet - requestsubscribemessage:fail can only be invoked by user tap gesture

UDP servers and clients in go

Nested call and chained access of functions in handwritten C language
随机推荐
Understand the module function of MES management system
81. 搜索旋转排序数组 II
Cloud, IPv6 and all-optical network
Developers, why does the maturity of container technology herald the arrival of cloud native era?
如何在IDEA中自定義模板、快速生成完整的代碼?
Preliminary understanding of NVIDIA Jetson nano
2022-06-29:x = { a, b, c, d }, y = { e, f, g, h }, x、y两个小数组长度都是4。 如果有: a + e = b + f = c + g = d + h
How to create a module in the idea and how to delete a module in the idea?
Three text to speech artifacts, each of which is very practical
快手伸手“供给侧”,找到直播电商的“源头活水”?
[MRCTF2020]Ezpop-1|php序列化
[deep learning compilation] operator compilation IR conversion
VIM编辑器常用指令
一文读懂,MES管理系统模块功能
Stimulus reports reporting tool, stimulus creates and builds reports
挖财的课程靠谱吗,让开户安全吗?
Vl6180x distance and light sensor hands-on experience
【Proteus仿真】8比特端口檢測8獨立按鍵
[concurrent programming] if you use channel to solve concurrency problems?
作文总写不好怎么办?猿辅导:家长要注意这几点

