当前位置:网站首页>力扣解法汇总535-TinyURL 的加密与解密
力扣解法汇总535-TinyURL 的加密与解密
2022-06-29 16:47:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
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 。
提示:
1 <= url.length <= 104
题目数据保证 url 是一个有效的 URL
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/encode-and-decode-tinyurl
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 缩短路径,那只能形成一个映射关系,这个映射关系是长链接和短链接的映射关系。 * 这种映射关系有很多种,我这里用最简单的一种,取长链接的MD5进行映射。
代码:
public class Solution535 {
public static class Codec {
Map<String, String> cache = new HashMap<>();
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
String key = getMD5String(longUrl);
cache.put(key, longUrl);
return key;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
return cache.get(shortUrl);
}
public String getMD5String(String str) {
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
//一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方)
return new BigInteger(1, md.digest()).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}边栏推荐
- 固态存储厂商忆联加入龙蜥社区,共建开源新生态
- 535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
- 关于onReachButton 不触发可能原因
- 解题元宇宙,网络游戏中的多元通信方案
- 6.26CF模拟赛D:黑白条题题解
- Implement a ThreadLocal by yourself
- Étalonnage de la caméra monoculaire et de la caméra binoculaire à l'aide de l'outil d'étalonnage kalibr
- 2022年有哪些适合穷人的理财产品?
- 最高81.98%!超百所“双一流”高校本科深造率公布
- What is the strength of a software testing engineer who can get a salary increase twice a year?
猜你喜欢

关于XAMPP无法启动mysql数据库

MySQL基础——多表查询

Accelerate the implementation of intelligent driving projects? You still lack a truth evaluation system

Metadata management Apache Atlas Compilation integration deployment and testing

一个简单但是能上分的特征标准化方法

C language -- printf print base prefix

使用kalibr標定工具進行單目相機和雙目相機的標定

【无标题】

Apache atlas breakpoint view

真正的测试 =“半个产品+半个开发”?
随机推荐
Huaxia Fund: sharing of digital transformation practice achievements in the fund industry
Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world
连续10年霸榜第一?程序员「最常用」的编程语言是它?
都3年测试经验了,用例设计还不知道状态迁移法?
Real test = "half product + Half development"?
New feature of C11 - Auto and decltype type type indicators
卷妹带你学数据库---5天冲刺Day4
或许再过两年,ASML将可以自由给中国供应EUV光刻机
It's also a test. Why do others get an annual salary of 30w+?
Redolog and binlog
PHP删除目录
UWB precise positioning scheme, centimeter level high-precision technology application, intelligent pairing induction technology
Practice | solution for image upload, rotation and compression on mobile terminal
6.26CF模拟赛D:黑白条题题解
Review of mathematical knowledge: curve integral of type I
数学知识:求组合数 II—求组合数
Which version of JVM is the fastest?
稳定币风险状况:USDT 和 USDC 安全吗?
What memory consuming data is stored in MySQL CDC jobmanager?
XAMPP Apache安装时问题总结