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

运行结果


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

相关推荐
孬甭_1 分钟前
从基础到优化:深入理解插入排序与希尔排序
数据结构·算法·排序算法
好家伙VCC3 分钟前
Rust+Bioinfo:80ms极速SNP注释引擎
java·开发语言·算法·rust
啦哈拉哈4 分钟前
【Python】知识点零碎学习7
python·学习·算法
宝贝儿好6 分钟前
【NLP】第八章:项目实操案例:文本情感分析
人工智能·python·深度学习·算法·自然语言处理
代码中介商7 分钟前
C++11右值引用与移动语义深度解析
开发语言·c++
码上有光8 分钟前
c++:二叉搜索树(map和set的底层结构)
开发语言·c++·递归·二叉搜索树
AF_INET614 分钟前
sensor笔记(一)imx415
c语言·经验分享·音视频·linux驱动·sensor·imx415·datasheet
如竟没有火炬15 分钟前
恢复二叉搜索树
数据结构·数据库·python·leetcode·动态规划
Brilliantwxx16 分钟前
【C++】 链式哈希表(Separate Chaining)
c++·哈希算法·散列表
如竟没有火炬19 分钟前
整数拆分——动态规划
开发语言·数据结构·python·算法·leetcode·动态规划