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

运行结果


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

相关推荐
皓月斯语23 分钟前
B3849 [GESP样题 三级] 进制转换 题解
c++·算法·题解
中微极客32 分钟前
剪枝与量化:让YOLO在边缘设备上高效部署
算法·yolo·剪枝
Ljwuhe41 分钟前
C++——模板进阶
开发语言·c++
hehelm41 分钟前
AI 大模型接入 SDK —项目概述
linux·服务器·网络·数据库·c++
牢姐与蒯1 小时前
双指针算法
数据结构·算法
Hesionberger1 小时前
LeetCode406:重建身高队列精髓解析
开发语言·数据结构·python·算法·leetcode
蜜桃味女焊匠人2 小时前
2026焊接保护气体降耗最优改造方案
人工智能·经验分享·其他·机器人
不要葱花2 小时前
接下来我将复现 10 篇强化学习算法:第 3 篇,一杯喜茶,搞定 Search-R1
算法·面试
geovindu2 小时前
CSharp: Recursion Algorithm
开发语言·后端·算法·c#·递归算法