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

运行结果


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

相关推荐
炽烈小老头12 小时前
【 每天学习一点算法 2026/05/14】单词接龙
学习·算法
yxc_inspire12 小时前
24年CCPC山东邀请赛补题
学习·算法
号码认证服务12 小时前
如何让来电显示公司名代替陌生数字号码?企业号码认证开通指南
服务器·c语言·网络·经验分享·智能手机·云计算·php
计算机安禾12 小时前
【c++面向对象编程】第19篇:多继承与菱形继承(二):虚拟继承的内存模型与复杂性
开发语言·c++
木子墨51612 小时前
工程算法实战 | 数据库ORDER BY的底层:内存排序 → 外部归并 → 索引优化
数据结构·数据库·python·sql·算法·动态规划
思麟呀12 小时前
在C++基础上理解CSharp-1
开发语言·c++·c#
学习,学习,在学习13 小时前
Q工控仪器程序框架设计详解(工控)
c++·qt·架构·qt5
j7~13 小时前
【Linux系统】基础IO(文件描述)(1)
linux·服务器·c++·文件·基础io
竹之月13 小时前
【Auto CAD 2020】单张打印输出PDF图纸A0、A1尺寸,黑白颜色
经验分享·pdf·auto cad2020
广州灵眸科技有限公司13 小时前
瑞芯微(EASY EAI)RV1126B 模型部署API说明
linux·开发语言·网络·人工智能·深度学习·算法·yolo