当前位置:网站首页>535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
2022-06-29 11:31:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 「535. TinyURL 的加密与解密」 ,难度为 「中等」。
Tag : 「哈希表」、「模拟」
TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。
请你设计一个类来加密与解密 TinyURL 。
加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。
实现 Solution 类:
Solution()初始化TinyURL系统对象。String encode(String longUrl)返回longUrl对应的TinyURL。String decode(String shortUrl)返回shortUrl原本的URL。题目数据保证给定的shortUrl是由同一个系统对象加密的。
示例:
输入:url = "https://leetcode.com/problems/design-tinyurl"
输出:"https://leetcode.com/problems/design-tinyurl"
解释:
Solution obj = new Solution();
string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。
string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
提示:
题目数据保证 url是一个有效的URL
模拟 + 哈希表
对于每个 longUrl 我们都在「大写字母/小写字母/数字」中随机 位作为其映射标识,这需要使用一个哈希表 tiny2Origin 进行记录。
同时了防止相同的 longUrl 多次调用,确保 encode 服务的「幂等性」,我们再额外建立哈希表 origin2Tiny 来记录原串和映射标识的对应关系。
代码:
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);
}
}
时间复杂度: encode操作复杂度为 ,其中 为短串长度, 为传入参数longUrl的长度(存入哈希表需要计算longUrl哈希值,该过程需要遍历longUrl);decode操作复杂度为 ,其中 为传入参数shortUrl的长度,该长度固定为prefix长度加空间复杂度: ,其中 为调用次数, 为平均 longUrl长度
最后
这是我们「刷穿 LeetCode」系列文章的第 No.535 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
- Equals increases execution speed / performance optimization
- 2022 construction worker civil engineering direction general foundation (construction worker) theoretical question bank simulation test platform operation
- 如何查看网站已经保存的密码
- GBase 8s 扩展外连接1
- [graduation season] summarize the past and look forward to the future
- Jerry's manual pairing method [chapter]
- 杰理之WiFi干扰蓝牙【篇】
- 在校生的编程故事
- ERP编制物料清单 华夏
- Sofaregistry source code | data synchronization module analysis
猜你喜欢

Weekly recommended short video: How did Einstein think?

普通用户使用vscode登录ssh编辑root文件
![[VTK] MFC grid editor based on vtk8.2](/img/c5/d0f070ccb819fc682855319b7415e0.png)
[VTK] MFC grid editor based on vtk8.2

Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5

The blackened honeysnow ice city wants to grasp the hearts of consumers by marketing?

论文复现——AC-FPN:Attention-guided Context Feature Pyramid Network for Object Detection.

当技术人成长为 CEO,应该修改哪些“Bug”?

& 4 express framework

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

SOFARegistry 源码|数据同步模块解析
随机推荐
Ttchat x Zadig open source co creates helm access scenarios, and environmental governance can be done!
& 4 express framework
GBase8s数据库在组合查询中的集合运算符
2022施工员-土建方向-通用基础(施工员)理论题库模拟考试平台操作
Jerry's about TWS channel configuration [chapter]
AOSP ~ 初始化语言
Oracle expands distributed cloud services to bring comprehensive public cloud services to more customers
pod安全策略(PSP)
Dragon Book tiger Book whale Book gnawing? Try the monkey book with Douban score of 9.5
Is the table queried by this EMR sparksql node ODPs?
现在怎么开户?有没有更快又安全的开通渠道
每周推荐短视频:爱因斯坦是怎样思考问题的?
Equals increases execution speed / performance optimization
Binary tree recursion and iteration
AutoCAD - text display mode and how CAD can directly open Tianzheng drawings
杰理之关于 TWS 声道配置【篇】
正大期货留4数据整合
一种可解释的几何深度学习模型,用于基于结构的蛋白质结合位点预测
跟着官方学电机,BLDC两种控制策略,学到即赚到
When a technician becomes a CEO, what "bugs" should be modified?