当前位置:网站首页>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
边栏推荐
- linux中mysql 1045错误如何解决
- 【R语言数据科学】:文本挖掘(以特朗普推文数据为例)
- 基于汇编实现的流载体的LSB隐藏项目
- LeetCode 每日一题——535. TinyURL 的加密与解密
- mysql视图能不能创建索引
- Mysql中锁的使用场景是什么
- The fixed assets management system enables enterprises to dynamically master assets
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- C语言练习----指针字符串、链表
- 卷妹带你学数据库---5天冲刺Day1
猜你喜欢

机器学习8-人工神经网络

Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool

Function calculation asynchronous task capability introduction - task trigger de duplication

ICML 2022 | 基于解耦梯度优化的可迁移模仿学习方法

mysql查询视图命令是哪个

Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
Help MySQL data analysis with databend

自旋电子学笔记-张曙丰

Problem solving metauniverse, multi communication scheme in online games

序列检测器
随机推荐
使用 SSH 方式拉取代码
0基础自学STM32(野火)——寄存器点亮LED
Online text digit recognition list summation tool
XAMPP Apache安装时问题总结
Pancakeswap Technology: development principle of gripper robot system
C语言练习----指针字符串、链表
About xampp unable to start MySQL database
Master slave replication of MySQL
What is the follow-up plan of infotnews | meta in the metauniverse?
What are the project management systems suitable for small and medium-sized enterprises?
About harbor private warehouse forgetting the login password
Open source warehouse contribution - submit pr
【Oracle】基础知识面试题
Sectigo ov pan domain name certificate is 1590 yuan a year easy to use
2020版KALI安装教程
Viewing splitchunks code segmentation from MPX resource construction optimization
如何在 PowerPoint 中向幻灯片添加 SmartArt?
sectigo ov泛域名证书一年一千五百九十元好用吗
PancakeSwap技术:夹子机器人系统开发原理
浏览器大尺寸截屏