当前位置:网站首页>[daily question] 535 Encryption and decryption of tinyurl
[daily question] 535 Encryption and decryption of tinyurl
2022-06-30 06:53:00 【Wang Liuliu's it daily】
535. TinyURL Encryption and decryption
Reference resources :https://leetcode.cn/problems/encode-and-decode-tinyurl/solution/tinyurl-de-jia-mi-yu-jie-mi-by-leetcode-ty5yp/
subject :
Encoding and decoding
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 .
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 .
Answer key :
About the short URL Common “ encryption ” Method or idea
- Self increasing id

String str = “ How do you do , I'm six six !”;
int i = str.lastIndexOf(“,”);The output is zero 3.
lastIndexOf The result of is the subscript of the character . Subscript from 0 Start .
String str2 = str.substring(str.lastIndexOf(“,”));
The output is zero :, I'm six six !
substring Start with this character .
public class Codec {
private Map<Integer, String> dataBase = new HashMap<Integer, String>();
private int id;
public String encode(String longUrl) {
id++;
dataBase.put(id, longUrl);
return "http://tinyurl.com/" + id;
}
public String decode(String shortUrl) {
int p = shortUrl.lastIndexOf('/') + 1;
int key = Integer.parseInt(shortUrl.substring(p));
return dataBase.get(key);
}
}
- Hash generation

public class Codec {
static final int K1 = 1117;
static final int K2 = 1000000007;
private Map<Integer, String> dataBase = new HashMap<Integer, String>();
private Map<String, Integer> urlToKey = new HashMap<String, Integer>();
public String encode(String longUrl) {
if (urlToKey.containsKey(longUrl)) {
return "http://tinyurl.com/" + urlToKey.get(longUrl);
}
int key = 0;
long base = 1;
for (int i = 0; i < longUrl.length(); i++) {
char c = longUrl.charAt(i);
key = (int) ((key + (long) c * base) % K2);
base = (base * K1) % K2;
}
while (dataBase.containsKey(key)) {
key = (key + 1) % K2;
}
dataBase.put(key, longUrl);
urlToKey.put(longUrl, key);
return "http://tinyurl.com/" + key;
}
public String decode(String shortUrl) {
int p = shortUrl.lastIndexOf('/') + 1;
int key = Integer.parseInt(shortUrl.substring(p));
return dataBase.get(key);
}
}
Random generation
public class Codec {
private Map<Integer, String> dataBase = new HashMap<Integer, String>();
private Random random = new Random();
public String encode(String longUrl) {
int key;
while (true) {
key = random.nextInt();
if (!dataBase.containsKey(key)) {
break;
}
}
dataBase.put(key, longUrl);
return "http://tinyurl.com/" + key;
}
public String decode(String shortUrl) {
int p = shortUrl.lastIndexOf('/') + 1;
int key = Integer.parseInt(shortUrl.substring(p));
return dataBase.get(key);
}
}
边栏推荐
- 银河麒麟初体验
- Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide
- CPU到底是怎么识别代码的?
- 与MQTT的初定情缘
- 【转】存储器结构、cache、DMA架构分析
- 不忘初心,能偷懒就偷懒:C#操作Word文件
- ROS program compilation, like no compilation, refers to the execution of the old compiled executable program
- 【Hot100】11. 盛最多水的容器
- RT thread migration to s5p4418 (IV): thread synchronization
- ROS system problem: rosdep init
猜你喜欢

第一行代码(第三版)学习笔记

【Mask-RCNN】基于Mask-RCNN的目标检测和识别

不忘初心,能偷懒就偷懒:C#操作Word文件

How to set the hot deployment of idea web project

It turns out that you are such an array. You have finally learned

GO安装以及配置(1)

经纬恒润再次荣获PACCAR集团 10PPM 质量奖

Porting RT thread to s5p4418 (V): thread communication

【我的创作纪念日】一周年随笔

汇编语言学习一(有栈协程铺垫,32位寄存器和相关指令学习,未完待续06/29)
随机推荐
经纬恒润再次荣获PACCAR集团 10PPM 质量奖
原来你是这样的数组,终于学会了
Improve simulation speed during ROS and Px4 joint simulation
ftplib+ tqdm 上传下载进度条
Practice summary of Prometheus project in amu Laboratory
memcpy内存重叠的解决
Rising posture series: fancy debugging information
SOC_AHB_SD_IF
[my creation anniversary] one year anniversary essay
C language: exercise 3
Bat usage details 2
Use of sscanf function
MySQL中的InnoDB引擎
手机开户一般哪个证券公司好?还有,在线开户安全么?
Go installation and configuration (1)
Ftplib+ tqdm upload and download progress bar
与MQTT的初定情缘
Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集
Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry
【我的创作纪念日】一周年随笔