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

运行结果


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

相关推荐
Learner__Q23 分钟前
每天五分钟:滑动窗口-LeetCode高频题解析_day3
python·算法·leetcode
J ..34 分钟前
C++ 多线程编程基础与 std::thread 使用
c++
你的冰西瓜35 分钟前
C++标准模板库(STL)全面解析
开发语言·c++·stl
阿昭L37 分钟前
leetcode链表相交
算法·leetcode·链表
闻缺陷则喜何志丹1 小时前
【计算几何】仿射变换与齐次矩阵
c++·数学·算法·矩阵·计算几何
chen_ever1 小时前
Protobuf详解(从安装到实战)
c++·rpc·信息与通信
liuyao_xianhui1 小时前
0~n-1中缺失的数字_优选算法(二分查找)
算法
做cv的小昊1 小时前
【TJU】信息检索与分析课程笔记和练习(1)认识文献
经验分享·笔记·学习·搜索引擎·全文检索
hmbbcsm1 小时前
python做题小记(八)
开发语言·c++·算法
斌蔚司李1 小时前
rayN 客户端使用说明
经验分享