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

运行结果


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

相关推荐
带多刺的玫瑰24 分钟前
Leecode#26刷题之删除有序数组中的重复项
数据结构·算法·leetcode
小七在进步39 分钟前
C++入门(2)
java·jvm·c++
en.en..1 小时前
C语言核心解析:#define与typedef本质区别
开发语言·c++·算法
码匠许师傅1 小时前
【设计模式精讲】26.策略模式(Strategy)
c++·设计模式·策略模式·uml
洋不写bug2 小时前
二叉树(二) 常见基础操作解析|结点数、树高、查找结点、判断完全二叉树
java·开发语言·数据结构·完全二叉树·二叉树结点数·树高·查找结点
果果燕2 小时前
实习笔记(六)主机按钮状态上报完整流程
开发语言·c++·php
数据库小学妹2 小时前
什么是关系型数据库?零基础用一张订单表讲透原理、SQL与ACID
经验分享·关系型数据库·国产数据库·acid·关系数据库·关系模型·主键外键
化雨成烟2 小时前
C++之AVL树
数据结构
YonyouHRSaaS3 小时前
AI视频面试系统定义、功能作用、品牌推荐、选择攻略
人工智能·面试·职场和发展·ai面试·视频面试·ai视频面试
是个西兰花3 小时前
网络基础1
linux·网络·c++·智能路由器