当前位置:网站首页>【每日一题】535. TinyURL 的加密与解密
【每日一题】535. TinyURL 的加密与解密
2022-06-30 06:48:00 【王六六的IT日常】
535. TinyURL 的加密与解密
参考:https://leetcode.cn/problems/encode-and-decode-tinyurl/solution/tinyurl-de-jia-mi-yu-jie-mi-by-leetcode-ty5yp/
题目:
编码与解码
TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。
请你设计一个类来加密与解密 TinyURL 。
实现 Solution 类:
- Solution() 初始化 TinyURL 系统对象。
- String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。
- String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。
题解:
关于短URL的常用的“加密”方法或者思想
- 自增id

String str = “你好啊,我是六六!”;
int i = str.lastIndexOf(“,”);输出的结果是3。
lastIndexOf的结果是该字符的下标。下标从0开始。
String str2 = str.substring(str.lastIndexOf(“,”));
输出的结果是:,我是六六!
substring是从该字符开始。
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);
}
}
- 哈希生成

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);
}
}
随机生成
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);
}
}
边栏推荐
- 基础刷题(一)
- ROS-URDF
- RT thread migration to s5p4418 (III): static memory pool management
- Connect to remote server
- C language: exercise 3
- Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
- 【我的创作纪念日】一周年随笔
- High performance distributed execution framework ray
- SOC project AHB_ SD_ Host controller design
- 【json-tutorial】第一章学习笔记
猜你喜欢

ROS-URDF

leetcode:98. 验证二叉搜索树

1.9 - 存储器的分类

Introduction to programming ape (11) -- structure

随机网络,无标度网络,小世界网络以及NS小世界的性能对比matlab仿真

神经网络入门

SOC_ AHB_ SD_ IF

tomorrow! "Mobile cloud Cup" competition air publicity will start!

RT thread migration to s5p4418 (IV): thread synchronization

RT thread migration to s5p4418 (III): static memory pool management
随机推荐
File transfer protocol, FTP file sharing server
High performance distributed execution framework ray
判断h5在两端是在微信环境还是企业微信环境
Install the components corresponding to setup
1.7 - CPU的性能指标
关注这场直播,了解能源行业双碳目标实现路径
Introduction to programming ape (11) -- structure
1.7 - CPU performance indicators
ROS program compilation, like no compilation, refers to the execution of the old compiled executable program
Redis cache
Introduction to neural networks
Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry
Deep learning --- the weight of the three good students' scores (3)
Initial love with mqtt
RT thread Kernel Implementation (I): threads and scheduling
Bat 使用细节2
NFS mount
No module named 'pyqt5 QtMultimedia‘
1.5 - 逻辑运算
SOC项目AHB_SD_HOST控制器设计