当前位置:网站首页>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;
}
}
}
}边栏推荐
- 工作流模块Jar包启动报错:liquibase – Waiting for changelog lock….
- Createstore for Redux source code analysis
- Adobe Premiere基础-批量素材导入序列-变速和倒放(回忆)-连续动作镜头切换-字幕要求(十三)
- Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
- Distributed | several steps of rapid read / write separation
- 3h精通OpenCV(七)-颜色检测
- mongoTemplate - distinct 使用
- js两个一维数组合并并去除相同项(整理)
- Jar package background startup and log output
- Longest XOR path (dfs+01trie)
猜你喜欢

Adobe Premiere基础-批量素材导入序列-变速和倒放(回忆)-连续动作镜头切换-字幕要求(十三)
![[webdriver] upload files using AutoIT](/img/69/8c27626d515976b47f1df4831d09c8.png)
[webdriver] upload files using AutoIT

Bloom filter:

Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)

Mysql database literacy, do you really know what a database is

ISO 32000-2 国际标准7.7
![Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]](/img/33/bda0a898bfe3503197026d1f62e851.png)
Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]

Precondition end of script headers or end of script output before headers

Proxmox VE Install 7.2

小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
随机推荐
ISO 32000-2 international standard 7.7
Xiaomai technology x hologres: high availability of real-time data warehouse construction of ten billion level advertising
最长异或路径(dfs+01trie)
Premature end of script headers 或 End of script output before headers
JS merge two 2D arrays and remove the same items (collation)
Lodash deep copy usage
Analyze the implementation principle of zero copy mechanism, applicable scenarios and code implementation
Proxmox VE Install 7.2
VMware安装ESXI
分布式 | 几步快速拥有读写分离
ABC253 D FizzBuzz Sum Hard(容斥定理)
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
NVIDIA installs the latest graphics card driver
Two controller layer interface authentication methods
软件快速交付真的需要以安全为代价吗?
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)
Does rapid software delivery really need to be at the cost of security?
Distributed | several steps of rapid read / write separation
Issue 42: is it necessary for MySQL to have multiple column partitions