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. 思考一个关于Lambda表达式做为linq条件的问题

    有一个集合如下 List<User> users = new List<User> { new User{Name = "1",Aget = 12}, ne ...

  2. 2.1 Java程序的构成

    2.1 Java程序的构成 2.1.1逻辑构成 Java源程序逻辑构成分为两大部分:程序头包的引用和类 的定义 1.程序头包的引用 主要是指引用JDK软件包自带的包,也可以是自己定义的类. 引用之后程 ...

  3. Error:Android Source Generator: [sdk] Android SDK is not specified.

    有时候使用intellij idea 带入android 项目,运行提示Error:Android Source Generator: [sdk] Android SDK is not specifi ...

  4. c# .net core 下的网络请求

    本文章是在VS2017的环境下,.net core 1.1版本以上. 在这期间,由于.net core 并不基于IIS,我们的过去的网络请求代码在.net core框架下,有可能会出现不兼容,报错的现 ...

  5. webpack使用

    Webpack是一个现代js应用的模块打包机.如果一个文件依赖另一个文件,webpack认为这就存在一个依赖关系.不管另一个文件是什么内容,image,css或js都被当作一个模块.Webpack从e ...

  6. web前端面试集锦(自己搜集的,如有错误请不吝赐教)

    css 1 浏览器兼容性 CSS hack(针对IE6-,IE7,IE8,IE9以及其他浏览器) ‘ * ’ : 所有的IE浏览器都能识别 说明:在标准模式中 “-″减号是IE6专有的hack “\9 ...

  7. Dom元素的Property和Attribute

    Attribute就是DOM节点自带的属性,例如html中常用的id.class.title.align等: 而Property是这个DOM元素作为对象,其附加的内容,例如childNodes.fir ...

  8. Expression 转化为sql(三) --自定义函数

    SQL 语句有很多函数如len(),now()等等.如何来生成这些函数.最近研究也写办法共大家参考. 一.首先建立一个建一个扩展类,控制只能允许这些函数出现,如果出现其他函数就直接报异常. publi ...

  9. javacv开发详解之1:调用本机摄像头视频(建议使用javaCV1.3版本)

    javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...

  10. Canvas学习系列二:Canvas的坐标系统

    上一章内容中我们对canvas元素有了一个初步的认识,在接下来的章节中我们会慢慢学习canvas中图形的绘制:但是在绘制之前我们先来看看canvas中的坐标系统,因为这样我们才能知道绘制的图形放在什么 ...