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

简介:

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,参见代码如下:

解法一:

public:

    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        url.push_back(longUrl);
        return "http://tinyurl.com/" + to_string(url.size() - 1);
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        auto pos = shortUrl.find_last_of("/");
        return url[stoi(shortUrl.substr(pos + 1))];
    }
    
private:
    vector<string> url;
};

上面这种方法虽然简单,但是缺点却很多,首先,如果接受到多次同一url地址,仍然会当做不同的url来处理。当然这个缺点可以通过将vector换成哈希表,每次先查找url是否已经存在。虽然这个缺点可以克服掉,但是由于是用计数器编码,那么当前服务器存了多少url就曝露出来了,也许会有安全隐患。而且计数器编码另一个缺点就是数字会不断的增大,那么编码的长度也就不是确定的了。而题目中明确推荐了使用六位随机字符来编码,那么我们只要在所有大小写字母和数字中随机产生6个字符就可以了,我们用哈希表建立6位字符和url之间的映射,如果随机生成的字符之前已经存在了,我们就继续随机生成新的字符串,直到生成了之前没有的字符串为止。下面的代码中使用了两个哈希表,目的是为了建立六位随机字符串和url之间的相互映射,这样进来大量的相同url时,就不用生成新的随机字符串了。当然,不加这个功能也能通过OJ,这道题的OJ基本上是形同虚设,两个函数分别直接返回参数字符串也能通过OJ,囧~

解法二:

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 = 0;
        string randStr;
        for (int i = 0; i < 6; ++i) randStr.push_back(dict[rand() % 62]);
        while (short2long.count(randStr)) {
            randStr[idx] = dict[rand() % 62];
            idx = (idx + 1) % 5;
        }
        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("/") + 1);
        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

本文转自博客园Grandyang,原文链接:[LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

,如需转载请自行联系原博主。

相关文章
|
5月前
|
Java
Java开发实现图片URL地址检验,如何编码?
【10月更文挑战第14天】Java开发实现图片URL地址检验,如何编码?
161 4
|
3天前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
30 11
|
7月前
|
算法 Python
【Leetcode刷题Python】百分号解码
深信服公司的算法笔试题.
72 1
|
7月前
|
API
【Azure API 管理】Azure API Management在设置 Policy时,如何对URL进行解码呢? 使用 HttpUtility.UrlDecode 出错
【Azure API 管理】Azure API Management在设置 Policy时,如何对URL进行解码呢? 使用 HttpUtility.UrlDecode 出错
|
9月前
|
存储 SQL 算法
优化解码方法:记忆化搜索和空间优化动态规划的实用指南 【LeetCode 题目 91】
优化解码方法:记忆化搜索和空间优化动态规划的实用指南 【LeetCode 题目 91】
|
8月前
|
JavaScript 前端开发 数据格式
URL编码【详解】——Javascript对URL进行编码解码的三种方式的区别和使用场景,axios请求拦截器中对get请求的参数全部进行URL编码
URL编码【详解】——Javascript对URL进行编码解码的三种方式的区别和使用场景,axios请求拦截器中对get请求的参数全部进行URL编码
424 0
|
9月前
|
安全 JavaScript PHP
URL百分号编码
URL百分号编码
|
10月前
|
数据采集 Python
python 如何url解码
【4月更文挑战第14天】
190 1
|
10月前
动态规划之解码方法【LeetCode】
动态规划之解码方法【LeetCode】
|
6月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行