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

运行结果


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

相关推荐
residual_fan4 分钟前
数据缺失填补算法之CUR-Estimator
算法·数据挖掘·数据分析
西西弗Sisyphus6 分钟前
C++ 实现 替代 OpenCV resize INTER_LINEAR 的一种方式
c++·opencv
wabs66610 分钟前
关于二叉树【力扣107.二叉树的层序遍历II的思考】
数据结构·c++·算法·leetcode·二叉树·层序遍历
知产xiao_xin23 分钟前
版权的精神权利
经验分享·笔记·知识产权
炼金士24 分钟前
客户导向型IT服务项目绩效测量的双仪表盘治理模型研究——基于责任边界与行为防御的视角
职场和发展
galaxy_strive24 分钟前
Qt C++插件化编写项目(1)
开发语言·c++·qt
见叶之秋36 分钟前
【C++】C++ 核心进阶(一):泛型编程基石 —— 模板初阶与 STL 体系开篇
开发语言·c++
一切皆是因缘际会1 小时前
资源的适配
大数据·人工智能·算法
小七在进步1 小时前
数据结构:非比较排序:计数排序
数据结构
鲸能云1 小时前
【智慧能源】源储调售一体化架构与 EMS 滚动调度算法实践 —— 工商业光储收益提升 22% 的技术拆解
算法·架构·能源