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

运行结果


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

相关推荐
库玛西13 分钟前
深入剖析 Linux 线程机制与分页式存储管理
linux·服务器·c++·笔记
奋发向前wcx14 分钟前
y1,y2总复习笔记2 2026.7.15
java·笔记·算法
天才测试猿15 分钟前
如何封装自动化测试框架?
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
ttod_qzstudio28 分钟前
【软考算法】软件设计师下午第四题算法总纲:从“直接放弃“到“稳稳拿分“
算法·软考
众少成多积小致巨40 分钟前
Android C++ 编码规范指南
android·c++·google
Yyyyyy~1 小时前
【C++】字符串
c++
G.O.G.O.G1 小时前
《LeetCode SQL 从入门到精通(MySQL)》09
sql·mysql·leetcode
广州灵眸科技有限公司1 小时前
xfce桌面旋转说明:基于灵眸科技EASY-EAI-Nano-TB
网络·网络协议·tcp/ip·算法·php
wWYy.1 小时前
Pimpl模式
c++
得物技术1 小时前
从"机械应答"到"服务伙伴":得物高可控智能客服的 Agent 工程实践|AICon 演讲整理
算法·llm·agent