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

运行结果


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

相关推荐
张李浩4 小时前
Leetcode 054螺旋矩阵 采用方向数组解决
算法·leetcode·矩阵
big_rabbit05024 小时前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
WolfGang0073214 小时前
代码随想录算法训练营 Day11 | 二叉树 part01
数据结构
美好的事情能不能发生在我身上4 小时前
Hot100中的:贪心专题
java·数据结构·算法
myloveasuka4 小时前
Java与C++多态访问成员变量/方法 对比
java·开发语言·c++
2301_821700535 小时前
C++编译期多态实现
开发语言·c++·算法
奥地利落榜美术生灬5 小时前
c++ 锁相关(mutex 等)
开发语言·c++
xixihaha13245 小时前
C++与FPGA协同设计
开发语言·c++·算法
程序员小远5 小时前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
小小怪7505 小时前
C++中的函数式编程
开发语言·c++·算法