当前位置:网站首页>LeetCode 535(C#)
LeetCode 535(C#)
2022-07-07 15:38:00 【有趣就行】
题目
TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk 。请你设计一个类来加密与解密 TinyURL 。
加密和解密算法如何设计和运作是没有限制的,你只需要保证一个 URL 可以被加密成一个 TinyURL ,并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。
实现 Solution 类:
Solution() 初始化 TinyURL 系统对象。
String encode(String longUrl) 返回 longUrl 对应的 TinyURL 。
String decode(String shortUrl) 返回 shortUrl 原本的 URL 。题目数据保证给定的 shortUrl 是由同一个系统对象加密的。
示例:
输入:url = “https://leetcode.com/problems/design-tinyurl”
输出:“https://leetcode.com/problems/design-tinyurl”
解释:
Solution obj = new Solution();
string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。
string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。
代码
- 直接寻址(哈希)
public class Codec
{
private const int K1 = 10007;
private const int K2 = 1000000007;
private Dictionary<string, int> url2Key = new Dictionary<string, int>();
private Dictionary<int, string> key2Url = new Dictionary<int, string>();
// Encodes a URL to a shortened URL
public string encode(string longUrl)
{
if (url2Key.ContainsKey(longUrl))
return "http://tinyurl.com/" + url2Key[longUrl];
int key = 0;
long b = 1;
foreach (char c in longUrl)
{
key = (int)((long)key * b + c) % K2;
b = (b * K1) % K2;
}
while (key2Url.ContainsKey(key))
key++;
url2Key.Add(longUrl, key);
key2Url.Add(key, longUrl);
return "http://tinyurl.com/" + key;
}
// Decodes a shortened URL to its original URL.
public string decode(string shortUrl)
{
int idx = shortUrl.LastIndexOf('/') + 1;
int key = int.Parse(shortUrl[idx..]);
return key2Url[key];
}
}
- 拉链寻址(哈希)
public class Codec
{
private const int K1 = 10007;
private const int K2 = 1000000007;
private Dictionary<string, (int, int)> url2Key =
new Dictionary<string, (int, int)>();
private Dictionary<int, IList<string>> key2Url =
new Dictionary<int, IList<string>>();
// Encodes a URL to a shortened URL
public string encode(string longUrl)
{
if (url2Key.ContainsKey(longUrl))
{
var key = url2Key[longUrl];
return "http://tinyurl.com/" + key.Item1.ToString() + '.' + key.Item2.ToString();
}
int key1 = 0;
long P = 1;
foreach (char c in longUrl)
{
key1 = (int)((long)(key1 * P + c) % K2);
P = P * K1 % K2;
}
if (!key2Url.ContainsKey(key1)) key2Url.Add(key1, new List<string>());
key2Url[key1].Add(longUrl);
int key2 = key2Url[key1].Count - 1;
url2Key.Add(longUrl, (key1, key2));
return "http://tinyurl.com/" + key1.ToString() + '.' + key2.ToString();
}
// Decodes a shortened URL to its original URL.
public string decode(string shortUrl)
{
string[] strs = shortUrl[(shortUrl.LastIndexOf('/') + 1)..].Split('.');
int key1 = int.Parse(strs[0]);
int key2 = int.Parse(strs[1]);
return key2Url[key1][key2];
}
}
边栏推荐
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- Flash build API Service - generate API documents
- LeetCode 1696. Jumping game VI daily question
- SIGGRAPH 2022最佳技术论文奖重磅出炉!北大陈宝权团队获荣誉提名
- 【饭谈】如何设计好一款测试平台?
- mysql使用笔记一
- skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
- Matplotlib绘图界面设置
- Sator推出Web3遊戲“Satorspace” ,並上線Huobi
- 【Seaborn】组合图表:PairPlot和JointPlot
猜你喜欢

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

mysql官网下载:Linux的mysql8.x版本(图文详解)

让保险更“保险”!麒麟信安一云多芯云桌面中标中国人寿, 助力金融保险信息技术创新发展

QT 图片背景色像素处理法

Nerf: the ultimate replacement for deepfake?

skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配

Sator launched Web3 game "satorspace" and launched hoobi

SIGGRAPH 2022最佳技术论文奖重磅出炉!北大陈宝权团队获荣誉提名
直接上干货,100%好评

QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
随机推荐
Lowcode: four ways to help transportation companies enhance supply chain management
DNS series (I): why does the updated DNS record not take effect?
Sator launched Web3 game "satorspace" and launched hoobi
QML初学
LeetCode 300. 最长递增子序列 每日一题
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
LeetCode 1986. 完成任务的最少工作时间段 每日一题
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
SlashData开发者工具榜首等你而定!!!
服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
Mrs offline data analysis: process OBS data through Flink job
Sator推出Web3游戏“Satorspace” ,并上线Huobi
《产品经理必读:五种经典的创新思维模型》的读后感
LeetCode 1654. The minimum number of jumps to get home one question per day
skimage学习(1)
Leetcode brush questions day49
LeetCode 1043. Separate the array to get the maximum and daily questions
[Seaborn] combination chart: pairplot and jointplot
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls