当前位置:网站首页>Leetcode daily question - 535 Encryption and decryption of tinyurl
Leetcode daily question - 535 Encryption and decryption of tinyurl
2022-06-29 17:17:00 【SK_ Jaco】
1. Title Description
535. TinyURL Encryption and decryption
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 .
2. Problem solving ideas and codes
2.1 Their thinking
This problem is to generate short links and parse short links . The title does not require short links , Then we can map by using a hash table . First, when coding, start from a-z、A-Z、0-9 The short link code is generated by randomly selecting six bits from , Then judge whether the code has been generated , If it already exists, regenerate , If it does not exist, the code and the original long link will be placed in map in . The decoding part takes out the encoding according to the short link , And according to the code from map Take out the long link .
2.2 Code
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 test result
Pass the test
3. summary
- Use random numbers to generate six bit short link codes
- Use hash table to cache encoding and mapping of long links
边栏推荐
- Fluent的msh格式网格学习
- When MySQL RDS is collected using Flink CDC, the datetime type field will be compared with the source table after collection
- Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool
- Gradle download slow or unable to download
- 开源仓库贡献 —— 提交 PR
- epoll分析
- flink sql rownumber 报错。谁遇到过啊?怎么解决?
- 535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
- 首批!腾讯云通过中国信通院政务协同平台解决方案能力评估
- InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
猜你喜欢
Collaborative development of epidemic home outsourcing project 𞓜 community essay solicitation
Fluent的msh格式网格学习
适合中小企业的项目管理系统有哪些?
【R语言数据科学】:文本挖掘(以特朗普推文数据为例)
SLAM中的子图
【南京大学】考研初试复试资料分享
@Difference between component and @configuration
Master slave replication of MySQL
Online sql to CSV tool
Naacl 2022 | distillation of machinetranslation SOTA model
随机推荐
微信小程序开发储备知识
0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解
如何在 PowerPoint 中向幻灯片添加 SmartArt?
C language practice ---- pointer string and linked list
第42期:MySQL 是否有必要多列分区
Redis principle - sorted set (Zset)
AI and creativity
Help MySQL data analysis with databend
regular expression
Summary of problems during xampp Apache installation
Master slave replication of MySQL
High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"
Interrupt怎么用
Redis布隆过滤器和布谷鸟过滤器
An error is reported in the Flink SQL rownumber. Who has met him? How to solve it?
腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化
外部自动(PLC启动机器人)
开源仓库贡献 —— 提交 PR
“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”
In depth analysis of Monai (I) data and transforms