LeetCode 535. TinyURL 的加密与解密(哈希)
发布日期:2021-07-01 03:14:08 浏览次数:2 分类:技术文章

本文共 708 字,大约阅读时间需要 2 分钟。

文章目录

1. 题目信息

TinyURL是一种URL简化服务, 比如:当你输入一个URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL http://tinyurl.com/4e9iAk.

要求:设计一个 TinyURL 的加密 encode 和解密 decode 的方法。你的加密和解密算法如何设计和运作是没有限制的,你只需要保证一个URL可以被加密成一个TinyURL,并且这个TinyURL可以用解密方法恢复成原本的URL。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/encode-and-decode-tinyurl

2. 哈希解题

  • 用简单的 int 映射成网址,进行转化
    在这里插入图片描述
class Solution {
int id; unordered_map
m;public: Solution():id(0){
} string encode(string longUrl) {
m[++id] = longUrl;//网址存入哈希表 return "http://"+to_string(id);//返回短网址 } string decode(string shortUrl) {
return m[stoi(shortUrl.substr(7,shortUrl.size()-7))]; //找到需要解码的子串并转成id,取哈希表里读取原网址 }};

转载地址:https://michael.blog.csdn.net/article/details/100565409 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode 257. 二叉树的所有路径(DFS)
下一篇:LeetCode 771. 宝石与石头(哈希)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年05月02日 10时47分56秒