当前位置:网站首页>Encryption and decryption of 535 tinyurl
Encryption and decryption of 535 tinyurl
2022-06-29 18:10:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
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) return longUrl Corresponding 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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/encode-and-decode-tinyurl
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * Shorten the path , That can only form a mapping relationship , This mapping relationship is the mapping relationship between long links and short links . * There are many kinds of mapping relationships , I use the simplest one here , Long linked MD5 mapping .
Code :
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 {
// Generate a MD5 Encryption computing abstract
MessageDigest md = MessageDigest.getInstance("MD5");
// Calculation md5 function
md.update(str.getBytes());
// digest() Finally, make sure to return to md5 hash value , The return value is 8 A string . because md5 hash The value is 16 Bit hex value , It's actually 8 A character
// BigInteger Function will 8 Bit string into 16 position hex value , To express in a string ; Get the string form of hash value
// One byte It's 8-bit binary , That is to say 2 Hexadecimal character (2 Of 8 The second power equals 16 Of 2 Power )
return new BigInteger(1, md.digest()).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
}
边栏推荐
- Find the maximum XOR value in the sequence given a number (01 Dictionary)
- 3h精通OpenCV(五)-透视变换
- Codeworks 5 questions per day (1700 for each) - the next day
- 最长异或路径(dfs+01trie)
- How QQ opens online customer service
- /usr/bin/ld: warning: **libmysqlclient.so.20**, needed by //usr/
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
- Top 30 open source software
- [wangdingbei 2020 Qinglong formation]areuserialz
- mongoTemplate - distinct 使用
猜你喜欢
国内酒店交易DDD应用与实践——理论篇
Serial port experiment based on stm32f103zet6 library function
[webdriver] upload files using AutoIT
Detailed introduction and Simulation of bitmap
QQ如何开通在线客服
【目标跟踪】|stark配置 win otb
Software testing - you may not understand the basic theoretical knowledge
Adobe Premiere基础-不透明度(混合模式)(十二)
Configure the local domain name through the hosts file
双亲委派机制
随机推荐
分布式 | 几步快速拥有读写分离
VMware安装ESXI
Two controller layer interface authentication methods
上班可以做副业
Set double click to run the jar file
JS merge two one-dimensional arrays and remove the same items (collation)
MATLAB 最远点采样(FPS)
Jar package background startup and log output
3h精通OpenCV(九)-最简单的人脸检测
3h精通OpenCV(五)-透视变换
ABC253 D FizzBuzz Sum Hard(容斥定理)
Longest XOR path (dfs+01trie)
Top 30 open source software
小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
If the evaluation conclusion of waiting insurance is poor, does it mean that waiting insurance has been done in vain?
Analyze the implementation principle of zero copy mechanism, applicable scenarios and code implementation
Mongotemplate - distinct use
Software testing - you may not understand the basic theoretical knowledge
3H proficient in opencv (VI) - image stacking
Bloom filter: