当前位置:网站首页>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 213. 打家劫舍 II 每日一题
- SlashData开发者工具榜首等你而定!!!
- QT视频传输
- 麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
- Smart logistics platform: make overseas warehouses smarter
- LeetCode 1031. 两个非重叠子数组的最大和 每日一题
- Proxmox VE重装后,如何无损挂载原有的数据盘?
- [image sensor] correlated double sampling CDs
- Sator launched Web3 game "satorspace" and launched hoobi
- Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
猜你喜欢
Shallow understanding Net core routing
PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
Mrs offline data analysis: process OBS data through Flink job
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
科普达人丨一文弄懂什么是云计算?
The top of slashdata developer tool is up to you!!!
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
如何在博客中添加Aplayer音乐播放器
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
Seaborn数据可视化
随机推荐
Notes on installing MySQL in centos7
自定义View必备知识,Android研发岗必问30+道高级面试题
《产品经理必读:五种经典的创新思维模型》的读后感
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
Nerf: the ultimate replacement for deepfake?
第九届 蓝桥杯 决赛 交换次数
LeetCode 152. 乘积最大子数组 每日一题
如何在软件研发阶段落地安全实践
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
[fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
Leetcode brush questions day49
A tour of grpc:03 - proto serialization / deserialization
Rpcms method of obtaining articles under the specified classification
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
[source code interpretation] | source code interpretation of livelistenerbus
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
LeetCode 213. 打家劫舍 II 每日一题