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

运行结果


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

相关推荐
SunShaoLu27 分钟前
实测7款思维导图工具图片导出能力:PNG/SVG/PDF全支持,附选型指南
职场和发展·效率工具·思维导图
稚南城才子,乌衣巷风流1 小时前
块状链表:数据结构详解与实现
数据结构·链表
Zachery Pole1 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米2 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
满天星83035772 小时前
【算法】最长递增子序列(三种解法)
算法
hold?fish:palm2 小时前
kv存储主从复制的设计与实现
c++·redis·后端
啦啦啦啦啦zzzz3 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法
charlie1145141913 小时前
现代C++工程实践 WeakPtr 实战(三):WeakPtrFactory 与「最后成员」惯用法
开发语言·c++·开源项目·现代c++
Ztt6666666663 小时前
六方壶不只看棱角,转得温和才耐看
经验分享·笔记·其他·百度·微信公众平台
日拱一卒——功不唐捐4 小时前
红黑树删除(C语言)
c语言·数据结构