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

运行结果


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

相关推荐
superman超哥2 分钟前
仓颉语言中异常处理入门的深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
ss27313 分钟前
线程池优雅关闭:线程池生命周期管理:四种关闭策略的实战对比
java·jvm·算法
天呐草莓13 分钟前
热传导方程
算法·matlab
wxdlfkj13 分钟前
从坐标系重构到算法收敛:以高性能LTP传感器突破圆周分布孔组位置度的即时检测瓶颈
算法·重构
不能只会打代码16 分钟前
蓝桥杯--生命之树(Java)
java·算法·蓝桥杯·动态规划·贪心
LYFlied16 分钟前
前端跨端技术全景解析:从本质到未来
前端·职场和发展·跨端
老臣软件19 分钟前
macOS 下载太慢?问题可能不在网速
经验分享·macos·mac·实用软件·网络下载管理器
MobotStone24 分钟前
三步高效拆解顶刊论文
算法
CreasyChan24 分钟前
unity射线与几何检测 - “与世界的交互”
算法·游戏·3d·unity·数学基础
天才测试猿28 分钟前
Postman常见问题及解决方法
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman