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

运行结果


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

相关推荐
数字生命卡兹克8 分钟前
深度实测GPT-6.2 Astra,OpenAI在偏科的道路上越走越远了。
经验分享
凌冰_9 小时前
数据结构 - 时间空间复杂度(一)
数据结构·算法·软件工程
Pniubi9 小时前
力扣2 . 两数相加
算法·leetcode·职场和发展
戴·菲瑜9 小时前
为什么PPO难以做出顶级棋类引擎
算法·博弈论·rl·ppo
德思特10 小时前
德思特测试Q&A | 多通道射频测试,如何实现高效分配与自动化调度?
经验分享·多通道射频测试
在所不辞兄12 小时前
为什么数字孪生驱动的产线标定需要在线更新机制,而不能一次标定后长期沿用?
人工智能·神经网络·其他·算法·机器学习
无忧.芙桃13 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
爱分享的康康14 小时前
高斯泼溅(3DGS)在 ADAS 仿真中的工程瓶颈与量产落地思路
经验分享
郝学胜-神的一滴15 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
文丑颜不良啊15 小时前
LeetCode3401~3420题目解析
java·leetcode