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

运行结果


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

相关推荐
lvchaoq2 分钟前
this指向面试代码输出题目合集
javascript·面试·职场和发展
小帽子_1231 小时前
大功率 PCS 双环闭环控制算法原理与实现
算法
智者知已应修善业1 小时前
【proteus交通灯故障检测的完善方案】2025-4-29
经验分享·笔记·单片机·嵌入式硬件·proteus
buhuizhiyuci1 小时前
【算法篇】位运算 —— 基础篇
java·数据库·算法
Black蜡笔小新2 小时前
算法训练完了怎么上线?DLTM→AIS→EasyGBS三步交付流水线
人工智能·算法
weixin_727535622 小时前
JVM 常见八股面试问答:深入底层原理
jvm·面试·职场和发展
孬甭_2 小时前
C++ string类
开发语言·c++
sycmancia2 小时前
Qt——多线程与界面组件的通信
开发语言·qt·算法
蜜桃味女焊匠人2 小时前
手工氩弧焊高耗气整改实战
人工智能·经验分享·其他·机器人
想吃火锅10052 小时前
【leetcode】5.最长回文子串js
算法·leetcode·职场和发展