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

运行结果


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

相关推荐
玛卡巴卡ldf4 分钟前
【LeetCode 手撕算法】(回溯)全排列DFS、子集、电话号码字母组合 九键、组合总和、括号生成、单词搜索、分割回文数
java·算法·leetcode·力扣
minji...7 分钟前
Linux 网络基础(三)HTTP的请求方法(GET/POST),HTTP表单、临时和永久重定向状态码、Cookie、查询参数、Web根目录
linux·运维·服务器·网络·c++·http
风筝在晴天搁浅7 分钟前
快手/腾讯 CodeTop LeetCode 43.字符串相乘
算法·leetcode
鱼很腾apoc8 分钟前
【学习篇】第18期 C++模板
c语言·c++
郝学胜-神的一滴10 分钟前
跨平台 C++ 静态库编译实战:Linux/Windows/macOS 三端统一实现
linux·开发语言·c++·windows·软件构建
派葛穆11 分钟前
ESP32开发- OLED显示“你好世界”——U8g2库完整教程
c++
tankeven16 分钟前
C++ 指针
c++
_深海凉_22 分钟前
LeetCode热题100-括号生成
算法·leetcode·职场和发展
bitbrowser25 分钟前
2026年防关联指纹浏览器评测天梯榜
经验分享
Eloudy26 分钟前
表面码中的CNOT 门的共轭变换规则
算法·量子计算