LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路
Posted HERODING23
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路相关的知识,希望对你有一定的参考价值。
解题思路:
直接map存储url,然后用一个随便的字符串+URL序号代替返回,作为加密字符串,解密直接获取序号返回对应的value即可,代码如下:
class Solution
private:
int id = 0;
unordered_map<int, string> mp;
public:
// Encodes a URL to a shortened URL.
string encode(string longUrl)
mp[id] = longUrl;
return "noway.com/" + to_string(id);
id ++;
// Decodes a shortened URL to its original URL.
string decode(string shortUrl)
int i = stoi(shortUrl.substr(10, shortUrl.size() - 10));
return mp[i];
;
// Your Solution object will be instantiated and called as such:
// Solution solution;
// solution.decode(solution.encode(url));
以上是关于LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路的主要内容,如果未能解决你的问题,请参考以下文章
535. Encode and Decode TinyURL - LeetCode
Leetcode 535: Encode and Decode TinyURL
LeetCode 解题思路:535.Encode and Decode TinyURL