当前位置:网站首页>【每日一题】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);
}
}
边栏推荐
- List in set (2)
- Install the components corresponding to setup
- 相关数据库问题提问。
- 安装setup对应的组件
- Hao looking for a job
- RT thread Kernel Implementation (II): critical area, object container
- Graphic octet, really top
- Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry
- 华泰炒股安全吗?我想网上开户。
- High performance distributed execution framework ray
猜你喜欢

深度学习---三好学生各成绩所占权重问题(3)

判断h5在两端是在微信环境还是企业微信环境

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

1.7 - CPU performance indicators

Connect to remote server

基础刷题(一)

SOC_ SD_ CLK

Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb

Fastapi learning Day1

Graphic octet, really top
随机推荐
记录一次腾讯测试开发工程师自动化接口测试实践经验
Four ways to create multithreads
Fastapi learning Day2
0基础转行软件测试,如何实现月薪9.5k+
NFS mount
Four tips in numpy
Imxq Freescale yocto project compilation record
1.6 - CPU composition
Getting started with research
RT thread Kernel Implementation (II): critical area, object container
ROS system problem: rosdep init
SOC_ AHB_ SD_ IF
ETL为什么经常变成ELT甚至LET?
与MQTT的初定情缘
KEIL - 下载调试出现“TRACE HW not present”
Suggestion: use tools:overrideLibrary
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
Collections tool class (V)
GO安装以及配置(1)
ROS multi machine