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

运行结果


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

相关推荐
RickyWasYoung8 小时前
【笔记】矩阵的谱半径
笔记·算法·矩阵
一分之二~8 小时前
回溯算法--递增子序列
开发语言·数据结构·算法·leetcode
m0_639397298 小时前
代码随想录算法训练营第五十天|图论理论基础,深搜理论基础,98. 所有可达路径,广搜理论基础
算法·图论
Yu_Lijing8 小时前
基于C++的《Head First设计模式》笔记——策略模式
c++·笔记·设计模式
鸿儒5178 小时前
记录一个C++操作8位影像的一个bug
开发语言·c++·bug
脏脏a8 小时前
深度剖析 C++ string:从 0 到 1 的模拟实现与细节解析
开发语言·c++
智驱力人工智能8 小时前
无人机车辆密度检测系统价格 询价准备 需要明确哪些参数 物流园区无人机车辆调度系统 无人机多模态车流密度检测技术
深度学习·算法·安全·yolo·无人机·边缘计算
福尔摩斯张8 小时前
【实战】C/C++ 实现 PC 热点(手动开启)+ 手机 UDP 自动发现 + TCP 通信全流程(超详细)
linux·c语言·c++·tcp/ip·算法·智能手机·udp
罗湖老棍子8 小时前
【例3-3】医院设置(信息学奥赛一本通- P1338)
数据结构·c++·算法·
不想写笔记8 小时前
算法 C语言 冒泡排序
c语言·笔记·算法·排序算法