▶ 要求给出一种对 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的更多相关文章

  1. 535. Encode and Decode TinyURL - LeetCode

    Question 535. Encode and Decode TinyURL Solution 题目大意:实现长链接加密成短链接,短链接解密成长链接 思路:加密成短链接+key,将长链接按key保存 ...

  2. LC 535. Encode and Decode TinyURL

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  3. 【Leetcode】535. Encode and Decode TinyURL

    Question: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/pro ...

  4. 535. Encode and Decode TinyURL 长短URL

    [抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...

  5. 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 ...

  6. [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  7. 【LeetCode】535. Encode and Decode TinyURL 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:数组 方法二:字典 日期 题目地址:https://l ...

  8. 535 Encode and Decode TinyURL 编码和解码精简URL地址

    详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...

  9. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

随机推荐

  1. Rails 5 Test Prescriptions 第9章 Testing-JavaScript: Integration Testing,❌挂一个问题webpacker::helper

    使用Capybara进行JS的集成测试 谈论驱动 让测试通过 Webpack in Development Mode Js设计 是用户在网页上有好的体验的重要因素. 尽管如此,许多网页不测试JS. 部 ...

  2. UVA-12166 Equilibrium Mobile(二叉树)

    题目大意:改变二叉树上的权值,使树平衡,问最少该几个值. 题目分析:不会做,查的题解.有条奇妙的性质:如果将第d层权值为w的节点为基准做改动,则整棵树的总重量为w<<d,即w*2^d.仔细 ...

  3. React中父子组件间的通信问题

    1.https://blog.csdn.net/sinat_17775997/article/details/59103173 (React中父子组件间的通信问题)

  4. C++设计模式之适配器模式

    对象适配器有以下特点: 有的时候,你会发现,不是很容易去构造一个Adaptee类型的对象: 当Adaptee中添加新的抽象方法时,Adapter类不需要做任何调整,也能正确的进行动作: 可以使用多肽的 ...

  5. Toncat-OpenSSL双向认证配置(iOS)

    OpenSSL生成证书 要生成证书的目录下建立几个文件和文件夹,有./demoCA/ ./demoCA/newcerts/ ./demoCA/private/ ./demoCA/index.txt ( ...

  6. STL标准库-容器-set与multiset

    技术在于交流.沟通,转载请注明出处并保持作品的完整性. set与multiset关联容器 结构如下 set是一种关联容器,key即value,value即key.它是自动排序,排序特点依据key se ...

  7. Echart参数详解收藏

    最全: https://www.cnblogs.com/Kqingniao/p/5833419.html 柱形图: https://blog.csdn.net/qq_36330228/article/ ...

  8. mysql配置调优-开启慢查询日志-slow_query_log

    工作中,会遇到需要查看mysql的top 20 慢sql,逐个进行优化,加上必要的索引这种需求,这时就需要开启数据库的慢查询日志的功能 1.查询当前慢查询日志的状态 # 默认为关闭状态 mysql - ...

  9. 来来来,有讲一个吐血的故事(matlab)之脚本运行路径是什么

    脚本运行路径是什么,这真是太重要!! 重要1:你默认保存的路径 重要2:你访问的相对路径 先放图: 再看一幅图: 我的操作,点击左侧的文件夹,使上框的显示栏路径不一样,再点击运行,发现pwd指示的路径 ...

  10. 每天一个linux命令(网络):【转载】route命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...