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));

运行结果


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

相关推荐
Navigator_Z3 分钟前
LeetCode //C - 1187. Make Array Strictly Increasing
c语言·算法·leetcode
火眼金睛炼单词5 分钟前
音标记单词:高效记忆的底层逻辑与实操方法
经验分享·高考
charlie1145141919 分钟前
IMX6ULL WM8960 的移植——前置介绍
开发语言·c++·开源项目·嵌入式linux
charlie11451419133 分钟前
Cinux是如何管理进程的 —— 上下文与调度
开发语言·c++·操作系统·开源项目
龍德明宇34 分钟前
如何理解大语言模型的负主体性-龍德明宇
人工智能·算法·大语言模型llm·负主体性·ai存在论
hold?fish:palm1 小时前
redis中AOF 重写机制解析
数据库·c++·redis
卡梅德生物科技小能手1 小时前
卡梅德生物科普 Zika Virus(寨卡病毒)|病毒结构与科研实验体系解析
经验分享·深度学习·生活
阿米亚波1 小时前
【C++ 异常处理】try-catch
开发语言·c++·笔记·try-catch
白狐_7981 小时前
408数据结构第6章:迪杰斯特拉(Dijkstra)算法——真题精讲
数据结构·算法
旖旎夜光1 小时前
C++(内存管理)
开发语言·c++·学习