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

运行结果


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

相关推荐
程序员敲代码吗几秒前
C++与硬件交互编程
开发语言·c++·算法
8K超高清几秒前
博冠8K广播级讯道摄像机获国际设计大奖
网络·算法·fpga开发·接口隔离原则·智能硬件
你怎么知道我是队长1 分钟前
C语言---排序算法1---冒泡排序法
c语言·算法·排序算法
qq_537562672 分钟前
C++与Java性能对比
开发语言·c++·算法
忆锦紫3 分钟前
图像锐化算法:Robert/Sobel/Laplacian锐化算法及MATLAB实现
图像处理·算法·计算机视觉·matlab
m0_686041613 分钟前
C++中的策略模式应用
开发语言·c++·算法
沉默-_-4 分钟前
力扣hot100普通数组(1)--C++
java·数据结构·算法·leetcode·数组
Once_day6 分钟前
C++之《Effective C++》读书总结(1)
c语言·c++·effective c++
wasp5208 分钟前
拒绝 OOM:Apache Fesod 高性能 Excel 处理架构全景解析
算法·架构·apache·excel
爱吃生蚝的于勒10 分钟前
【Linux】进程信号的保存(二)
linux·运维·服务器·c语言·数据结构·c++·算法