当前位置:网站首页>LeetCode 535(C#)
LeetCode 535(C#)
2022-07-07 19:17:00 【Just be interesting】
subject
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 .
There is no limit to how encryption and decryption algorithms are designed and operated , You just need to guarantee one URL Can be encrypted into a TinyURL , And this TinyURL It can be restored to the original by decryption URL .
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 .
Example :
Input :url = “https://leetcode.com/problems/design-tinyurl”
Output :“https://leetcode.com/problems/design-tinyurl”
explain :
Solution obj = new Solution();
string tiny = obj.encode(url); // Returns the result of encryption TinyURL .
string ans = obj.decode(tiny); // Returns the original after decryption URL .
Code
- Direct addressing ( Hash )
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];
}
}
- Zipper addressing ( Hash )
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];
}
}
边栏推荐
- Draw squares with Obama (Lua)
- LeetCode 890(C#)
- 前首富,沉迷种田
- 3.关于cookie
- Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible
- 完整的电商系统
- AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
- 如何给“不卖笔”的晨光估值?
- Interview vipshop internship testing post, Tiktok internship testing post [true submission]
- 高温火烧浑不怕,钟薛高想留清白在人间
猜你喜欢

2022.07.02

微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案

从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
![[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question](/img/08/5f4b4e2700606554516807c01454fd.png)
[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question

PV静态创建和动态创建

链式二叉树的基本操作(C语言实现)

Cadre de validation des données Apache bval réutilisé

ES6 note 1

In the first half of 2022, I found 10 books that have been passed around by my circle of friends

Antisamy: a solution against XSS attack tutorial
随机推荐
POJ 1182 :食物链(并查集)[通俗易懂]
Big Ben (Lua)
In 2021, the national average salary was released. Have you reached the standard?
Flipping Game(枚举)
Basic concepts and properties of binary tree
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
Desci: is decentralized science the new trend of Web3.0?
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Business experience in virtual digital human
Continuous test (CT) practical experience sharing
【Base64笔记】「建议收藏」
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Redis集群与扩展
Number - number (Lua)
Classification and application of enterprise MES Manufacturing Execution System
App capture of charles+postern
Comparison and selection of kubernetes Devops CD Tools
Mathematical analysis_ Notes_ Chapter 11: Fourier series
Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution
炒股如何开户?请问一下手机开户股票开户安全吗?