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

运行结果


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

相关推荐
上海广测检测科技有限公司几秒前
LoRa模块SRRC型号核准判定解析:微功率目录边界与核准触发条件
经验分享
代码不停3 分钟前
BFS算法解决FloodFill问题
算法·bfs·宽度优先
艾莉丝努力练剑5 分钟前
【AI大模型接入SDK】ChatSDK架构设计与实现
网络·c++·人工智能·学习·语言模型
重生之小比特10 分钟前
【C++进阶】红黑树的实现
java·开发语言·c++
纪念 22911 分钟前
C++算法(一)
开发语言·c++·算法
All for pursuit.30 分钟前
【二叉树-10】114.二叉树展开为链表
数据结构·c++·算法·leetcode·链表
叠层归一研究院32 分钟前
区分预算与通道诱导:Ω–L叠层体系的观测赋值与定量验证
人工智能·算法
多弗朗皮卡丘35 分钟前
C++string类
c++
文飞AI智能写作1 小时前
文献阅读笔记怎么做,写综述时才能找到依据
经验分享·大学生·论文写作·文飞ai
BYSJMG1 小时前
计算机毕业设计选题推荐:基于大数据的快递物流运营数据分析与可视化,Spark与K-Means
大数据·算法·数据分析·spark·课程设计