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地址的更多相关文章

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

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

  2. [LeetCode] 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: Encode and Decode TinyURL

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

  4. 535. Encode and Decode TinyURL - LeetCode

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

  5. LC 535. Encode and Decode TinyURL

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

  6. [LeetCode] Design TinyURL 设计精简URL地址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

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

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

  8. 【Leetcode】535. Encode and Decode TinyURL

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

  9. [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 ...

随机推荐

  1. virtualbox主机与虚拟机之间互相通信教程

    前言 在使用虚拟机搭建集群时,需要实现虚拟机与虚拟机之间互相ping通,并且主机与虚拟机也可以互相ping通. 一.环境准备: 1.主机为win7 2.virtualbox下创建两台ubuntu虚拟机 ...

  2. 2018.3.29 DIV位置调整代码

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  3. g第十四周,十五周作业

    1.数组中偶数的和 #include <stdio.h> int main(){ ; ]; ;i<=;i++) { scanf("%d ",&a[i]); ...

  4. 集大1513 & 1514班 软件工程第一次作业评分与点评

    谢谢大多数同学按时完成了作业,同学态度都比较端正,没有为了完成作业或者讨好老师而说一些假话空话. 很多同学选择CS之前并没有从兴趣或者擅长出发.这是一个普遍的现象,十年前我们是这样,十年后的孩子们还是 ...

  5. 2017-2018-1 20155205 实现mypwd

    2017-2018-1 20155205 实现mypwd 课堂总结 根据上课对ls -l功能的实现,我总结了实现一个linux命令需要的步骤: 使用man -k xx | grep xx查看帮助文档, ...

  6. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  7. 去掉xcode编译warning:ld: warning: directory not found for option '-L

    选择工程, 编译的 (targets) 选择 Build Settings 菜单 查找 Library Search Paths 和 Framework Search Paths, 删掉编译报warn ...

  8. JAVA_SE基础——21.二维数组的定义

    2 二维数组的定义 基本与一维数组类似 //定义一个3行5列的二维数组 //方法1,先new对象,然后再初始化每个元素 int[][] a = new int[3][5]; a[0][0]=1; a[ ...

  9. python小练习之三---购物车程序

    购物车购物的例子 严格来讲,这个例子相对大一些 功能也稍完备一些,具有用户登录,商品上架,用户购物,放入购物车,展示每个用户的购物车里的商品的数量,用户账户余额,支持用户账户充值等 下面展示的代码有些 ...

  10. ViurtualBox配置虚拟机Linux的网络环境

    之前可以使用VMware配置成功,让虚拟机和本地通信,虚拟机可以访问外网,但是VMware体积太大了,最后终于把virtualBox也配置成功,也使得两者兼备 环境:本地windows7 64位专业版 ...