当前位置:网站首页>[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);
}
}
边栏推荐
- Which securities company is good for opening a mobile account? Also, is it safe to open an account online?
- 1.4 - 定点数与浮点数
- 1.8 - multi level storage
- 不忘初心,能偷懒就偷懒:C#操作Word文件
- 1.7 - CPU performance indicators
- 【json-tutorial】第一章学习笔记
- Huawei full-scale Daniel shared the 598 page full-color Manual of network protocols for the first time
- Redis cache
- 原来你是这样的数组,终于学会了
- Hao looking for a job
猜你喜欢
随机推荐
RT thread Kernel Implementation (II): critical area, object container
手机开户一般哪个证券公司好?还有,在线开户安全么?
元宇宙由哪些底层技术支撑?
InnoDB engine in MySQL
Basic questions (I)
RT thread application
1.8 - multi level storage
Gazebo installation, uninstall and upgrade
Pycharm shortcut key
Numpy中的四个小技巧
【Mask-RCNN】基于Mask-RCNN的目标检测和识别
HuaWei满级大牛首次分享出这份598页网络协议全彩手册
Practice summary of Prometheus project in amu Laboratory
Ls1028 manual
ROS service communication programming
leetcode:98. 验证二叉搜索树
CPU到底是怎么识别代码的?
Graphic octet, really top
Introduction to programming ape (11) -- structure
神经网络入门









