当前位置:网站首页>LeetCode 每日一题——535. TinyURL 的加密与解密
LeetCode 每日一题——535. TinyURL 的加密与解密
2022-06-29 17:12:00 【SK_Jaco】
1.题目描述
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 。
2.解题思路与代码
2.1 解题思路
这道题就是要生成短链接以及解析短链接。题目对短链接没要求,那么我们就可以通过使用哈希表来进行映射。首先在编码时从 a-z、A-Z、0-9 中随机抽取六位生成短链接编码,然后判断该编码是否已生成过,如果已存在则重新生成,如果不存在则将编码和原长链接放入 map 中。解码部分则根据短链接取出编码,并根据编码从 map 中取出长链接即可。
2.2 代码
public class Codec {
private Map<String, String> map = new HashMap<>();
private final static String TINY_URL = "http://tinyurl.com/";
private final static String RANDOM_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String encode = null;
do {
encode = generate();
} while (map.containsKey(encode));
map.put(encode,longUrl);
return TINY_URL+encode;
}
private String generate() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 6; i++) {
builder.append(RANDOM_POOL.charAt((int) (Math.random() * RANDOM_POOL.length())));
}
return builder.toString();
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return map.get(shortUrl.substring(TINY_URL.length()));
}
}
2.3 测试结果
通过测试

3.总结
- 使用随机数生成六位短链接编码
- 使用哈希表缓存编码和长链接的映射
边栏推荐
- 在线文本数字识别列表求和工具
- 如何在 PowerPoint 中向幻灯片添加 SmartArt?
- What role does the supply chain management system play in the supply chain scenario?
- 手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
- 适合中小企业的项目管理系统有哪些?
- 【现代信号处理第六次作业】
- controller、service、dao之间的关系
- 2022 software evaluator examination outline
- 固定资产管理系统让企业动态掌握资产情况
- @Component与@Configuration区别
猜你喜欢

What is the follow-up plan of infotnews | meta in the metauniverse?

0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解

Advanced MySQL - storage engine

curl: (56) Recv failure: Connection reset by peer

Calibration of monocular camera and binocular camera with kalibr calibration tool

Naacl 2022 | distillation of machinetranslation SOTA model

Tencent cloud released the upgraded version of CDW Clickhouse to provide a fast experience for massive data real-time analysis scenarios

「科普大佬说」AI与创造力

Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?

适合中小企业的项目管理系统有哪些?
随机推荐
GNN notes: message propagation model
Kubernetes部署Dashboard(WEB UI管理界面)
关于harbor私有仓库忘记登录密码
Actual combat | magical conic gradient
When MySQL RDS is collected using Flink CDC, the datetime type field will be compared with the source table after collection
XAMPP Apache安装时问题总结
固定资产管理系统让企业动态掌握资产情况
Kali installation tutorial 2020
垃圾收集器
广发易淘金和东方财富哪个更好,更安全一些
如何在 PowerPoint 中向幻灯片添加 SmartArt?
KUKA机器人外部轴配置你一定要知道的那些知识
线段树、树状数组模板(复制粘贴确实好用)
卷妹带你学jdbc—2天冲刺Day1
关于KALI使用xshell连接
High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"
Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?
使用kalibr标定工具进行单目相机和双目相机的标定
可转债策略之---(摊饼玩法,溢价玩法,强赎玩法,下修玩法,双低玩法)
Étalonnage de la caméra monoculaire et de la caméra binoculaire à l'aide de l'outil d'étalonnage kalibr