Leetcode—535. TinyURL 的加密与解密【中等】

2024每日刷题(110)

Leetcode---535. TinyURL 的加密与解密

实现代码

cpp 复制代码
class Solution {
public:

    // Encodes a URL to a shortened URL.
    string encode(string longUrl) {
        while(!urlToCode.count(longUrl)) {
            string code;
            for(int i = 0; i < 6; i++) {
                code += alphabets[rand() % alphabets.size()];
            }
            if(!codeToUrl.count(code)) {
                urlToCode[longUrl] = code;
                codeToUrl[code] = longUrl;
                return "http://tinyurl.com/" + code;
            }
        }
        throw;
    }

    // Decodes a shortened URL to its original URL.
    string decode(string shortUrl) {
        return codeToUrl[shortUrl.substr(19)];
    }
private:
    const string alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    unordered_map<string, string> urlToCode;
    unordered_map<string, string> codeToUrl;
};

// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));

运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
ting94520002 小时前
Humalike X Hermes 深度技术剖析:单指令注入群聊社交智能的底层架构、算法与跨 IM 平台实现
人工智能·算法·架构
豆沙沙包?2 小时前
C++~~~stack容器、queue容器、list容器(p45-P56)
c++·windows·list
吴声子夜歌2 小时前
Java面试——算法
java·算法·面试
evans在进步2 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
Frank_refuel2 小时前
【C++八股】面向对象
开发语言·c++
h_a_o777oah2 小时前
【图论】Tarjan 缩点:解决有向图中环的问题
c++·算法·图论·acm·强连通分量·缩点·tarjan
Tisfy2 小时前
LeetCode 1386.安排电影院座位:哈希表+位运算
算法·leetcode·散列表·题解·哈希表
Nil2083 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
OpenDataLab3 小时前
厦门大学“先进封装电子电镀配方研发智能体”入选上海 AI 实验室“与书生共创”十大成果,并接入 Sciverse 科学智能数据基座
经验分享
M78佐菲4 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法