当前位置:网站首页>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.总结
- 使用随机数生成六位短链接编码
- 使用哈希表缓存编码和长链接的映射
边栏推荐
猜你喜欢

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

Calibration of monocular camera and binocular camera with kalibr calibration tool

mysql数据库扫盲,你真的知道什么是数据库嘛

知道创宇为能源行业资产管理助力,入选工信部2021物联网示范项目

微信小程序开发储备知识

c# 国内外ORM 框架 dapper efcore sqlsugar freesql hisql sqlserver数据常规插入测试性能对比

Fluent的msh格式网格学习

Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)

PCB板框的绘制——AD19

A simple but scalable feature normalization method
随机推荐
Information | Zuckerberg was rated as the most careless CEO in the global IT industry; China Mobile R & D tethered UAV emergency communication high altitude base station
Fluent的msh格式网格学习
High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"
在线文本数字识别列表求和工具
NVIDIA安装最新显卡驱动
Interrupt怎么用
Simulink simulation mode
C语言微博用户管理系统
windows平台下的mysql启动等基本操作
SAAS 服务的优势都有哪些
A simple but scalable feature normalization method
基于C语言开发实现的一个用户级线程库
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
解题元宇宙,网络游戏中的多元通信方案
【R语言数据科学】:文本挖掘(以特朗普推文数据为例)
[R language data science]: Text Mining (taking Trump's tweet data as an example)
controller、service、dao之间的关系
6.26CF模拟赛D:黑白条题题解
6.26cf simulation game d: black and white questions
微信小程序开发储备知识