535. Encode and Decode TinyURL
▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9iAk,在不允许打表的情况下应该无法压缩的。
● 代码,6 ms,完全不压缩,just for fun
class Solution
{
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl) { return longUrl; }
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) { return shortUrl; }
};
class Solution
{
public:
string theLatestUrl;
string encode(string longUrl) { theLatestUrl = longUrl; return "whatever, doesn't matter"; }
string decode(string shortUrl) { return theLatestUrl; }
};
● 代码,9 ms,随机数打表法,加密之后的字符串只与原网址字符串在表中的存放位置有关,与其内容无关。类似的有各种打表法,需要依赖散列表来解码,时间复杂度 O(1),空间开销大。
class Solution
{
public:
unordered_map<string, string> um;
string encode(string longUrl)
{
const string baseUrl = "http://tinyurl.com/";
int r;
string ran;
srand();
for (r = rand() % , ran = to_string(r); um.count(baseUrl + ran) == ; r = rand() % , ran = to_string(r));
// 随机取一个空的位置把网址放进去
um[baseUrl + ran] = longUrl;
return baseUrl + ran;
}
string decode(string shortUrl)
{
if (um.count(shortUrl) == )
return um[shortUrl];
return "";
}
};
535. Encode and Decode TinyURL的更多相关文章
- 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】535. Encode and Decode TinyURL
Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...
- 535. Encode and Decode TinyURL(rand and srand)
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- [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】535. Encode and Decode TinyURL 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...
- 535 Encode and Decode TinyURL 编码和解码精简URL地址
详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- git 使用和安装
http://www.git-scm.com/download/ http://www.git-scm.com/download/win http://www.git-scm.com/download ...
- echarta3 北京,上海地图
1.首先你得到echarts官网下载js,建议下载完整代码,这样你就很容易根据我的路径找到beijing.js 2.把echarts.js和beijingi.js根据你的路径引对,然后就可以copy我 ...
- cdq分治的小结
cdq分治 是一种特殊的分治 他的思想: 1.分治l,mid 2.分治mid+1,r 3.计算l,mid对mid+1,r的影响 3就是最关键的地方 这也是cdq的关键点 想到了这一步基本就可以做了 接 ...
- Xilinx SDK使用教程
本文参考 Xilinx SDK软件内置的教程,打开方法:打开SDK->Help->Cheet Sheets...->Xilinx SDK Tutorials,这里有6篇文档.本文详细 ...
- sqlite常用语法详细介绍
1.SQL语句的预编译:将语句转为数据流,执行语句前检查语句的语法,但不能知道语句是否能查出结果.此方法有返回值 预编译成功则返回SQLITE_OK----0否则返回SQLITE_ERROR---- ...
- Python PIL : IOError: decoder jpeg not available
The first thing I check when I got this error was to check if libjpeg was installed. Lets try this s ...
- vue中assets和static的区别
Vue中assets和static的区别 再一次框架定型中,与同事在静态资源的存放上有了一些分歧,后来经过查阅总结如下: 相同点: assets和static两个都是存放静态资源文件.项目中所需要 ...
- AS中几个较好的插件
Android ButterKnife Zelezny 自动生成依赖注入代码 Android Parcelable code generator 自动生成Parcelable代码 Selecto ...
- 解决使用SecureCRT出现的Generic clipboard failure错误【自己亲身经历】
2016年11月8日:[解决办法]把金山词霸卸载了 血的教训浪费了好几个小时 相关文章 1.RecureCRT could not get data from the Clipboard 和SAP快捷 ...
- c# 爬虫(二) 模拟登录
有了上一篇的介绍,这次我们来说说模拟登录,上一篇见 :c# 爬虫(一) HELLO WORLD 原理 我们知道,一般需要登录的网站,服务器和客户端都会有一段时间的会话保持,而这个会话保持是在登录时候建 ...