[LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL.
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
这道题让我们编码和解码精简URL地址,这其实很有用,因为有的链接地址特别的长,就很烦,如果能精简成固定的长度,就很清爽。最简单的一种编码就是用个计数器,当前是第几个存入的url就编码成几,然后解码的时候也能根据数字来找到原来的url,参见代码如下:
解法一:
class Solution {
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
url.push_back(longUrl);
return "http://tinyurl.com/" + to_string(url.size() - );
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
auto pos = shortUrl.find_last_of("/");
return url[stoi(shortUrl.substr(pos + ))];
}
private:
vector<string> url;
};
上面这种方法虽然简单,但是缺点却很多,首先,如果接受到多次同一url地址,仍然会当做不同的url来处理。当然这个缺点可以通过将vector换成哈希表,每次先查找url是否已经存在。虽然这个缺点可以克服掉,但是由于是用计数器编码,那么当前服务器存了多少url就曝露出来了,也许会有安全隐患。而且计数器编码另一个缺点就是数字会不断的增大,那么编码的长度也就不是确定的了。而题目中明确推荐了使用六位随机字符来编码,那么我们只要在所有大小写字母和数字中随机产生6个字符就可以了,我们用哈希表建立6位字符和url之间的映射,如果随机生成的字符之前已经存在了,我们就继续随机生成新的字符串,直到生成了之前没有的字符串为止。下面的代码中使用了两个哈希表,目的是为了建立六位随机字符串和url之间的相互映射,这样进来大量的相同url时,就不用生成新的随机字符串了。当然,不加这个功能也能通过OJ,这道题的OJ基本上是形同虚设,两个函数分别直接返回参数字符串也能通过OJ,囧~
解法二:
class Solution {
public:
Solution() {
dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
short2long.clear();
long2short.clear();
srand(time(NULL));
}
// Encodes a URL to a shortened URL.
string encode(string longUrl) {
if (long2short.count(longUrl)) {
return "http://tinyurl.com/" + long2short[longUrl];
}
int idx = ;
string randStr;
for (int i = ; i < ; ++i) randStr.push_back(dict[rand() % ]);
while (short2long.count(randStr)) {
randStr[idx] = dict[rand() % ];
idx = (idx + ) % ;
}
short2long[randStr] = longUrl;
long2short[longUrl] = randStr;
return "http://tinyurl.com/" + randStr;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
string randStr = shortUrl.substr(shortUrl.find_last_of("/") + );
return short2long.count(randStr) ? short2long[randStr] : shortUrl;
}
private:
unordered_map<string, string> short2long, long2short;
string dict;
};
参考资料:
https://discuss.leetcode.com/topic/81637/two-solutions-and-thoughts/2
https://discuss.leetcode.com/topic/81736/c-solution-using-random-just-for-fun
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址的更多相关文章
- 535 Encode and Decode TinyURL 编码和解码精简URL地址
详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...
- [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- Leetcode: Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 535. Encode and Decode TinyURL - LeetCode
Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...
- LC 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- [LeetCode] Design TinyURL 设计精简URL地址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- 【Leetcode】535. Encode and Decode TinyURL
Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
随机推荐
- 关于input内容改变的触发时间
1.onchange onchange 事件会在域的内容改变时触发.支持的标签<input type="text">, <textarea>, <se ...
- [日常] NOIWC 2018爆零记
开个坑慢慢更(逃 (然而没准会坑掉?) day 0 大概 $8:30$ 就滚去雅礼了qwq 过去的时候发现并没有人...进报到处楼门的时候还被强行拍照围观了一波OwO 然后就领了HZ所有人的提包和狗牌 ...
- 关于如何在mac系统上安装Git并在码市上建立项目
对Git一窍不通,为了在mac系统上安装Git,查了很多资料,走了很多弯路,一切搞定后发现其实很简单. 1.在https://brew.sh上按要求安装Homebrew. 2.在电脑终端键入brew ...
- bisect 二分查找
先说明的是,使用这个模块的函数前先确保操作的列表是已排序的. 先看看 insort 函数: 其插入的结果是不会影响原有的排序. 再看看 bisect 函数: 其目的在于查找该数值将会插入的位置并返 ...
- Lucene 的索引文件锁原理
Lucene 的索引文件锁原理 2016/11/24 · IT技术 · lucene 环境 Lucene 6.0.0Java “1.8.0_111”OS Windows 7 Ultimate 线程 ...
- 201421123042 《Java程序设计》第2周学习总结
1. 本周学习总结 以几个关键词描述本周的学习内容.并将关键词之间的联系描述或绘制出来. 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴. 引用类型 引用类型是指向一个对象,感觉 ...
- Hibernate之Hibernate的下载与安装
Hibernate用法十分简单,当我们在Java项目中引入Hibernate框架之后,就能以面向对象的方式来操作关系数据库了. 下载: 登陆Hibernate官网,下载Hibernate压缩包,win ...
- GPUImage滤镜效果翻译
#import"GPUImageBrightnessFilter.h"//亮度 #import"GPUImageExposureFilter.h"//曝光 #i ...
- shell中冒号 : 用途说明
我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等.其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令.只起到占一 ...
- nyoj VF
VF 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make ...