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.

思路:
encode : 将网址映射到一个id上,使用hash map,保存对应的id和网址。

decode:用短网址计算出id,然后返回对应的长网址。

string dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int id = ;
unordered_map<string, string>m;
unordered_map<int, string>idm;
// Encodes a URL to a shortened URL.
string encode(string longUrl)
{
if (m.find(longUrl) != m.end())return m[longUrl];
string res = "";
id++;
int count = id;
while (count > )
{
res = dict[count % ] + res;
count /= ;
}
while (res.size() < ) res = "" + res;
m[longUrl] = res;
idm[id] = longUrl;
return res;
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl)
{
int id = ;
for (int i = ; i < shortUrl.size();i++)
{
id = * id + (int)(dict.find(shortUrl[i]));
}
if (idm.find(id) != idm.end())return idm[id];
return "";
}

参考:

https://discuss.leetcode.com/topic/81588/c-solution

[leetcode-535-Encode and Decode TinyURL]的更多相关文章

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

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

  2. 535. Encode and Decode TinyURL - LeetCode

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

  3. LC 535. Encode and Decode TinyURL

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

  4. 【Leetcode】535. Encode and Decode TinyURL

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

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

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

  6. 535. Encode and Decode TinyURL

    ▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...

  7. 535. Encode and Decode TinyURL 长短URL

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

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

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

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

  10. [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. 在Caffe上运行Cifar10示例

    准备数据集 在终端上运行以下指令: cd caffe/data/cifar10 ./get_cifar10.sh cd caffe/examples/cifar10 ./create_cifar10. ...

  2. 在Adapter中如何关闭当前Activity

    很多时候我们需要在点击adapter条目的时候关闭当前界面,把所点击条目的信息带到前一个界面,同时关闭当前界面. 这个 时候就需要在adapter对某一个点击事件做处理. 代码如下: holder.l ...

  3. (入门篇 NettyNIO开发指南)第四章-TIP黏包/拆包问题解决之道

    熟悉TCP编程的读者可能都知道,无论是服务端还是客户端,当我们读取或者发送消息的时候,都需要考虑TCP底层的粘包/拆包机制.木章开始我们先简单介绍TCP粘包/拆包的基础知识,然后模拟一个没有考虑TCP ...

  4. CefSharp使用入门

    首先这是很重要的,环境搭建: 我用的是VS2017 步骤            方法 1.            打开VS的安装管理器 2.             进入修改界面,使用C++的桌面开发 ...

  5. 元类(meta class)

    元类(meta class),这个名字想必很多人都听过,网上也有很多关于元类的介绍,今天我就按照自己这两天的理解来简单探讨一下这个玩意,有误之处还望指出. 首先,下载objc源码,源码地址:https ...

  6. 磁盘分区-gdisk用法

    gdisk用法 gdisk - InteractiveGUIDpartitiontable (GPT) manipulator GPTfdisk (akagdisk) isatext-modemenu ...

  7. CSS小技巧-两个盒子之间的间距问题

    1.水平排放的盒子,水平间距是两个margin的累加 2.垂直排放的盒子,垂直间距是合并的取最大值

  8. git使用3

    如何使用/学习第三方框架? 优秀的第三方框架都在 github.com 1> 搜索 2> git clone 获得完整版本 $ git clone https://github.com/A ...

  9. sleep()

    sleep() 方法可以使当前线程(即调用该方法的线程)暂停执行一段时间, 让其他线程有机会继续执行, 但它并不释放对象锁: 所以当sleep()方法结束时: 当前线程还是拥有对象锁: 当线程拥有对象 ...

  10. FLAnimatedImageView处理gif过程

    FLAnimatedImageView处理gif过程 时间控制原理 GIF图片每一帧的delayTime可能都不一样: 在展示下一帧的时间控制机制,不能根据以第一帧为准: 或总动画时长除以帧数来简单做 ...