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

运行结果


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

相关推荐
旖-旎几秒前
《LeetCode 646 最长数对链 || LeetCode 1143 最长公共子序列》
c++·算法·leetcode·动态规划
Frostnova丶18 分钟前
(15)LeetCode 189. 轮转数组
数据结构·算法·leetcode
柠小Q1 小时前
远程回退后推送本地变更全攻略
经验分享
Lumos1861 小时前
故障保护与鲁棒性(十七)
算法
阿米亚波1 小时前
【C++ STL】std::list
c++·windows·笔记·stl·list
橙色阳光五月天1 小时前
CMake 构建配置文件解析
c++·cmake·plugin
手写码匠1 小时前
从零实现大模型推理引擎:Continuous Batching 与动态调度系统深度解析
人工智能·深度学习·算法·aigc
载数而行5201 小时前
C++进阶 Linux相关的静态库,动态库,第三方库
c++
wabs6661 小时前
关于单调栈【力扣503.下一个更大元素II的思考】
算法·单调栈
c238561 小时前
下篇:伸缩魔法!进阶吃透可变滑动窗口篇
c++·算法