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

运行结果


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

相关推荐
我命由我123454 分钟前
Visual Studio - Visual Studio 注释快捷键
java·c语言·开发语言·c++·ide·java-ee·visual studio
小哈蒙德15 分钟前
基于deepSeekV4Pro(thinking)研究pointPillar的历程
python·算法
兰令水16 分钟前
topcode【随机算法题】【2026.5.16打卡-java版本】
java·数据结构·算法
NashSKY17 分钟前
关于支持向量机(SVM)的数学原理、参数拟合、嵌入式部署的完整指南
c++·python·机器学习·支持向量机
Shan120517 分钟前
广度优先搜索之层序遍历
数据结构·算法·宽度优先
99乘法口诀万物皆可变22 分钟前
面向电池管理系统(BMS)的 C++ 实时仿真内核
开发语言·c++
huaiixinsi22 分钟前
Java 后端面试高频题整理(02)
java·开发语言·spring·面试·职场和发展·架构·maven
SilentSamsara24 分钟前
自定义上下文管理器实战:数据库连接池、文件锁与超时控制
开发语言·python·算法·青少年编程
清辞85331 分钟前
集合竞价选股程序开发学习
数据结构
吃着火锅x唱着歌32 分钟前
LeetCode 503.下一个更大元素II
算法·leetcode·职场和发展